<?php

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

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreServiceComplaintRequest;
use App\Http\Requests\UpdateServiceComplaintRequest;
use App\Http\Resources\Admin\ServiceComplaintResource;
use App\Models\ServiceComplaint;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

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

        return new ServiceComplaintResource(ServiceComplaint::with(['client', 'location', 'asset', 'service_type', 'visit_type', 'status', 'enginner_assigned', 'supervisor_assigned', 'team'])->get());
    }

    public function store(StoreServiceComplaintRequest $request)
    {
        $serviceComplaint = ServiceComplaint::create($request->all());

        return (new ServiceComplaintResource($serviceComplaint))
            ->response()
            ->setStatusCode(Response::HTTP_CREATED);
    }

    public function show(ServiceComplaint $serviceComplaint)
    {
        abort_if(Gate::denies('service_complaint_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return new ServiceComplaintResource($serviceComplaint->load(['client', 'location', 'asset', 'service_type', 'visit_type', 'status', 'enginner_assigned', 'supervisor_assigned', 'team']));
    }

    public function update(UpdateServiceComplaintRequest $request, ServiceComplaint $serviceComplaint)
    {
        $serviceComplaint->update($request->all());

        return (new ServiceComplaintResource($serviceComplaint))
            ->response()
            ->setStatusCode(Response::HTTP_ACCEPTED);
    }

    public function destroy(ServiceComplaint $serviceComplaint)
    {
        abort_if(Gate::denies('service_complaint_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $serviceComplaint->delete();

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