<?php

namespace App\Http\Controllers\V3\Dashboard\SafetyAndPrivacy;

use App\Enums\SafetyAndPrivacyStatus;
use App\Exports\V3\SafetyAndPrivacy\SupervisorCartableReport;
use App\Http\Controllers\Controller;
use App\Http\Requests\V3\SafetyAndPrivacy\RejectRequest;
use App\Http\Traits\ApiResponse;
use App\Models\SafetyAndPrivacy;
use App\Services\Cartables\SafetyAndPrivacy\SupervisorService;
use App\Services\Cartables\SafetyAndPrivacyTableService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Throwable;

class SupervisorController extends Controller
{
    use ApiResponse;

    /**
     * @throws Throwable
     */
    public function index(Request $request, SupervisorService $supervisorService): JsonResponse
    {
        return response()->json($supervisorService->dataTable($request));
    }

    /**
     * @throws Throwable
     */
    public function excelReport(Request $request, SupervisorService $supervisorService): BinaryFileResponse
    {
        $name = 'گزارش از نگهداری حریم راه ' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
        $data = $supervisorService->dataTable($request);
        return Excel::download(new SupervisorCartableReport($data['data']), $name);
    }

    public function confirm(SafetyAndPrivacy $safetyAndPrivacy, Request $request): JsonResponse
    {
        $status_id = SafetyAndPrivacyStatus::CONFIRM->value;

        $safetyAndPrivacy->update([
            'status' => $status_id,
            'status_fa' => SafetyAndPrivacyStatus::name($status_id),
            'supervisor_description' => $request->supervisor_description,
            'supervisor_id' => auth()->user()->id,
            'supervisor_name' => auth()->user()->name,
        ]);

        return $this->successResponse();
    }

    public function reject(RejectRequest $request, SafetyAndPrivacy $safetyAndPrivacy): JsonResponse
    {
        $status_id = SafetyAndPrivacyStatus::REJECT->value;

        $safetyAndPrivacy->update([
            'status' => $status_id,
            'status_fa' => SafetyAndPrivacyStatus::name($status_id),
            'supervisor_description' => $request->supervisor_description,
            'supervisor_id' => auth()->user()->id,
            'supervisor_name' => auth()->user()->name,
        ]);

        return $this->successResponse();
    }
}
