<?php

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

use App\Exceptions\ProhibitedAction;
use App\Exports\V3\RoadItemsProjects\SupervisorCartableReport;
use App\Http\Controllers\Controller;
use App\Http\Requests\V3\RoadItem\Supervisor\ConfirmRequest;
use App\Http\Requests\V3\RoadItem\Supervisor\RejectRequest;
use App\Http\Requests\V3\RoadItem\VerifyBySupervisorRequest;
use App\Http\Traits\ApiResponse;
use App\Models\RoadItemsProject;
use App\Services\Cartables\RoadItem\SupervisorService;
use App\Services\Cartables\RoadItemTableService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
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
    {
        $data = $supervisorService->dataTable($request);

        foreach ($data['data'] as $road_item) {
            if (Gate::allows('gate-supervise-road-item', $road_item)) {
                $road_item['can_supervise'] = 1;
            } else {
                $road_item['can_supervise'] = 0;
            }
        }

        return response()->json($data);
    }

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

    public function confirm(ConfirmRequest $request, RoadItemsProject $roadItemsProject): JsonResponse
    {
        $user = auth()->user();

        DB::transaction(function () use ($request, $roadItemsProject, $user) {
            $roadItemsProject->update([
                'status' => 1,
                'status_fa' => 'تایید شده',
                'supervisor_id' => $user->id,
                'supervisor_name' => $user->name,
                'supervisor_description' => $request->description,
                'supervising_time' => now(),
            ]);

            $user->addActivityComplete(1149);
        });

        return $this->successResponse();
    }

    public function reject(RejectRequest $request, RoadItemsProject $roadItemsProject): JsonResponse
    {
        $user = auth()->user();

        DB::transaction(function () use ($request, $roadItemsProject, $user) {
            $roadItemsProject->update([
                'status' => 2,
                'status_fa' => 'عدم تایید',
                'supervisor_id' => $user->id,
                'supervisor_name' => $user->name,
                'supervisor_description' => $request->description,
                'supervising_time' => now(),
            ]);

            $user->addActivityComplete(1149);
        });

        return $this->successResponse();
    }

    /**
     * @throws Throwable
     */
    public function restore(RoadItemsProject $roadItemsProject): JsonResponse
    {

        throw_if($roadItemsProject->status == 0, new ProhibitedAction('امکان بازگردانی وضعیت این فعالیت وجود ندارد!'));

        DB::transaction(function () use ($roadItemsProject) {
            $roadItemsProject->update([
                'status' => 0,
                'status_fa' => 'در حال بررسی',
                'supervisor_id' => null,
                'supervisor_description' => null,
                'supervising_time' => null,
                'supervisor_name' =>  null,
            ]);
            auth()->user()->addActivityComplete(1150);
        });

        return $this->successResponse();
    }
}
