<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class WebsiteController extends Controller
{
    public function index(Request $request)
    {
        return response()->json(
            $request->user()->websites()->latest()->get()
        );
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'url' => 'required|url|max:2048'
        ]);

        $website = $request->user()->websites()->create([
            'url' => $validated['url'],
            'status' => 'idle'
        ]);

        return response()->json($website, 201);
    }

    public function bulkStore(Request $request)
    {
        $validated = $request->validate([
            'urls' => 'required|array',
            'urls.*' => 'required|url|max:2048'
        ]);

        $websites = [];
        foreach ($validated['urls'] as $url) {
            $websites[] = $request->user()->websites()->create([
                'url' => $url,
                'status' => 'idle'
            ]);
        }

        return response()->json(['message' => 'Websites added successfully', 'websites' => $websites], 201);
    }

    public function destroy(Request $request, $id)
    {
        $website = $request->user()->websites()->findOrFail($id);
        
        if ($website->screenshot_path) {
            \Illuminate\Support\Facades\Storage::disk('public')->delete($website->screenshot_path);
        }
        
        $website->delete();
        return response()->json(['message' => 'Website deleted successfully']);
    }

    public function scan(Request $request, $id)
    {
        $website = $request->user()->websites()->findOrFail($id);
        
        $website->update(['status' => 'pending']);
        \App\Jobs\CaptureScreenshotJob::dispatch($website);

        return response()->json(['message' => 'Scan queued', 'website' => $website]);
    }

    public function retry(Request $request, $id)
    {
        $website = $request->user()->websites()->findOrFail($id);

        if ($website->status !== 'failed') {
            return response()->json(['message' => 'Only failed scans can be retried.'], 422);
        }

        $website->update([
            'status'        => 'pending',
            'error_message' => null,
        ]);

        \App\Jobs\CaptureScreenshotJob::dispatch($website);

        return response()->json(['message' => 'Retry queued', 'website' => $website->fresh()]);
    }

    public function bulkScan(Request $request)
    {
        $validated = $request->validate([
            'ids' => 'required|array',
            'ids.*' => 'required|exists:websites,id'
        ]);

        $websites = $request->user()->websites()->whereIn('id', $validated['ids'])->get();

        foreach ($websites as $website) {
            $website->update(['status' => 'pending']);
            \App\Jobs\CaptureScreenshotJob::dispatch($website);
        }

        return response()->json(['message' => 'Bulk scan queued']);
    }
}
