<?php

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

use App\Facades\DataTable\DataTableFacade;
use App\Http\Controllers\Controller;
use App\Http\Requests\V3\Azmayesh\StoreRequest;
use App\Http\Requests\V3\Azmayesh\UpdateRequest;
use App\Http\Traits\ApiResponse;
use App\Models\Azmayesh;
use App\Models\AzmayeshType;
use App\Models\Province;
use Illuminate\Database\QueryException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AzmayeshController extends Controller
{
    use ApiResponse;
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request): JsonResponse
    {
        $columns = array(
            'id',
            'request_date',
            'report_date',
            'request_number',
            'employer',
            'contractor',
            'work_number',
            'consultant',
            'applicant',
            'azmayesh_type_name',
            'project_name',
            'updated_at',
            'created_at',
            'user_name',
            'contract_subitem_id'
        );

        $allowedFilters = $columns;
        $allowedSortings = $columns;

        $query = Azmayesh::query();

        $data = DataTableFacade::run(
            $query,
            $request,
            allowedFilters: $allowedFilters,
            allowedSortings: $allowedSortings);

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreRequest $request): JsonResponse
    {
        $azmayeshType = AzmayeshType::query()->findOrFail($request->azmayesh_type_id);
        $province = Province::query()->findOrFail($request->province_id);
        $user = auth()->user();

        Azmayesh::query()->create([
            'lat' => $request->lat,
            'lng' => $request->lng,
            'azmayesh_type_id' => $azmayeshType->id,
            'azmayesh_type_name' => $azmayeshType->name,
            'request_date' => $request->request_date,
            'report_date' => $request->report_date,
            'request_number' => $request->request_number ?? '-',
            'employer' => $request->employer ?? '-',
            'contractor' => $request->contractor ?? '-',
            'work_number' => $request->work_number ?? '-',
            'project_name' => $request->project_name,
            'consultant' => $request->consultant ?? '-',
            'applicant' => $request->applicant ?? '-',
            'province_id' => $province->id,
            'province_name' => $province->name_fa,
            'user_id' => $user->id,
            'user_name' => $user->name,
            'contract_subitem_id' => $request->contract_subitem_id
        ]);

        $user->addActivityComplete(1158);

        return $this->successResponse();
    }

    /**
     * Display the specified resource.
     */
    public function show(Azmayesh $azmayesh): JsonResponse
    {
        return $this->successResponse($azmayesh->load('azmayeshSamples'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateRequest $request, Azmayesh $azmayesh): JsonResponse
    {
        $province = Province::query()->findOrFail($request->province_id);
        $azmayeshType = AzmayeshType::query()->findOrFail($request->azmayesh_type_id);

        $azmayesh->update([
            'lat' => $request->lat,
            'lng' => $request->lng,
            'azmayesh_type_id' => $azmayeshType->id,
            'azmayesh_type_name' => $azmayeshType->name,
            'request_date' => $request->request_date,
            'report_date' => $request->report_date,
            'request_number' => $request->request_number ?? '-',
            'employer' => $request->employer ?? '-',
            'contractor' => $request->contractor ?? '-',
            'work_number' => $request->work_number ?? '-',
            'project_name' => $request->project_name,
            'consultant' => $request->consultant ?? '-',
            'applicant' => $request->applicant ?? '-',
            'province_id' => $province->id,
            'province_name' => $province->name_fa,
            'contract_subitem_id' => $request->contract_subitem_id
        ]);

        auth()->user()->addActivityComplete(1159);

        return $this->successResponse();
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Azmayesh $azmayesh): JsonResponse
    {
        try {
            $azmayesh->delete();

            auth()->user()->addActivityComplete(1160);

            return $this->successResponse();
        }
        catch (QueryException $exception) {
            return $this->errorResponse('امکان حذف وجود ندارد');
        }
    }
}
