<?php

namespace App\Http\Controllers;

use App\Models\PrintJob;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Printer;
use App\Models\Branch;
use App\Models\Restaurant;

class PrintJobController extends Controller
{
    public function testConnection(Request $request)
    {
        $branch = $request->get('branch');
        if ($branch) {
            $pusherSettings = pusherSettings();

            $pusherEnabled = $pusherSettings->is_enabled_pusher_broadcast;

            $response = [
                'message' => 'Connection established',
                'status' => 'success',
                'pusher_enabled' => $pusherEnabled,
            ];

            // Include Pusher configuration for desktop application
            if ($pusherEnabled) {
                $response['pusher_config'] = [
                    'app_id' => $pusherSettings->pusher_app_id,
                    'key' => $pusherSettings->pusher_key,
                    'cluster' => $pusherSettings->pusher_cluster ?? 'mt1',
                    'channel' => 'print-jobs',
                    'event' => 'print-job.created'
                ];
            }

            return response()->json($response, 200);
        }
        return response()->noContent(); // 204
    }

    public function printerDetails(Request $request)
    {
        $branch = $request->get('branch');
        $printer = Printer::where('branch_id', $branch->id)->get();
        return response()->json($printer);
    }

    /**
     * Get print jobs for a specific printer
     * Desktop applications can use this to get printer-specific jobs
     */
    public function getPrinterJobs(Request $request, $printerId)
    {
        $branch = $request->get('branch');

        $printJobs = PrintJob::where('printer_id', $printerId)
            ->where('branch_id', $branch->id)
            ->where('status', 'pending')
            ->with('printer:id,name,printing_choice,print_format,share_name,type')
            ->orderBy('created_at', 'asc')
            ->get();

        return response()->json([
            'success' => true,
            'print_jobs' => $printJobs,
            'count' => $printJobs->count()
        ]);
    }
    // Returns the oldest pending job (or 204 if none)
    public function pull(Request $request)
    {
        $branch = $request->get('branch');

        $job = PrintJob::with('printer:id,name,printing_choice,print_format,share_name,type')->where('status', 'pending')
            ->where('branch_id', $branch->id)
            ->oldest()
            ->first();

        if (!$job) {
            return response()->json(['message' => 'No pending jobs', 'status' => 'error'], 204);
        }


        $job->update(['status' => 'printing']);
        // dd($branch);
        return response()->json($job);
    }

    // Electron calls this after attempting to print
    public function update(Request $request, PrintJob $printJob)
    {
        $branch = $request->get('branch');

        $request->validate([
            'status'      => 'required|in:done,failed',
            'printed_at'  => 'nullable|date',
            'error'       => 'nullable|string',
            'printer'     => 'nullable|string',
        ]);
        $printJob->update([
            'status'     => $request->status,
            'response_printer'    => $request->printer,
            'printed_at' => $request->printed_at,
        ]);

        return response()->json(['message' => 'Print job updated', 'status' => 'success']);
    }
}
