<?php

namespace App\Http\Controllers\Api\V1\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreChecklistReportRequest;
use App\Http\Requests\UpdateChecklistReportRequest;
use App\Http\Resources\Admin\ChecklistReportResource;
use App\Models\ChecklistReport;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ChecklistReportApiController extends Controller
{
    public function index()
    {
        abort_if(Gate::denies('checklist_report_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return new ChecklistReportResource(ChecklistReport::with(['complaint', 'checklist_name', 'checklist_status_fusion_types_1', 'checklist_status_fusion_types_2', 'team'])->get());
    }

    public function store(StoreChecklistReportRequest $request)
    {
        $checklistReport = ChecklistReport::create($request->all());

        return (new ChecklistReportResource($checklistReport))
            ->response()
            ->setStatusCode(Response::HTTP_CREATED);
    }

    public function show(ChecklistReport $checklistReport)
    {
        abort_if(Gate::denies('checklist_report_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return new ChecklistReportResource($checklistReport->load(['complaint', 'checklist_name', 'checklist_status_fusion_types_1', 'checklist_status_fusion_types_2', 'team']));
    }

    public function update(UpdateChecklistReportRequest $request, ChecklistReport $checklistReport)
    {
        $checklistReport->update($request->all());

        return (new ChecklistReportResource($checklistReport))
            ->response()
            ->setStatusCode(Response::HTTP_ACCEPTED);
    }

    public function destroy(ChecklistReport $checklistReport)
    {
        abort_if(Gate::denies('checklist_report_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $checklistReport->delete();

        return response(null, Response::HTTP_NO_CONTENT);
    }
}
