<?php

namespace App\Http\Controllers\V3\Harim;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Harim\Divarkeshi;
use App\Http\Requests\V3\Harim\Divarkeshi\StoreRequest;
use App\Http\Traits\ApiResponse;
use Illuminate\Http\JsonResponse;
use App\Facades\DataTable\DataTableFacade;


class DivarkeshiController extends Controller
{
    use ApiResponse;

    public function index(Request $request): JsonResponse
    {
        $columns = array(
            'dabirkhaneh_number',
            'nameh_date',
            'nameh_date_fa',
            'marjae_pasokh',
            'motaghazi_is_legal_id',
            'motaghazi_is_legal',
            'motaghazi_type_id',
            'motaghazi_type',
            'motaghazi_firstname',
            'motaghazi_lastname',
            'national_id',
            'shenase_melli',
            'tel_number',
            'mobile_number',
            'address',
            'edare_kol_id',
            'edare_kol',
            'edare_shahri_id',
            'edare_shahri',
            'rah_type_id',
            'rah_type',
            'name_mehvar_id',
            'name_mehvar_fa',
            'mizan_harim',
            'arze_navar',
            'samt_id',
            'samt',
            'kilometr',
            'lat',
            'lon',
            'zone',
            'karbari_type_id',
            'karbari_type',
            'tarh_title',
            'masahat_zirbana',
            'ehdasat_type_id',
            'ehdasat_type',
            'divarkeshi_distance',
            'mostahadesat_distance',
            'mahale_ejra_id',
            'mahale_ejra',
            'traffic_id',
            'traffic',
            'vaziat_eghtesadi_id',
            'vaziat_eghtesadi',
            'has_access_id',
            'has_access',
            'shomare_estelam_harim',
            'max_month',
            'max_day',
            'shomare_tahaodname',
            'shomare_daftarkhaneh',
            'description',
            'ronevesht',
        );

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

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

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

    }

    public function store(StoreRequest $request): JsonResponse
    {
        try {
            Divarkeshi::create([
                'dabirkhaneh_number' => $request->dabirkhaneh_number,
                'nameh_date' => $request->nameh_date,
                'nameh_date_fa' => $request->nameh_date_fa,
                'marjae_pasokh' => $request->marjae_pasokh,
                'motaghazi_is_legal_id' => $request->motaghazi_is_legal_id,
                'motaghazi_is_legal' => $request->motaghazi_is_legal,
                'motaghazi_type_id' => $request->motaghazi_type_id,
                'motaghazi_type' => $request->motaghazi_type,
                'motaghazi_firstname' => $request->motaghazi_firstname,
                'motaghazi_lastname' => $request->motaghazi_lastname,
                'national_id' => $request->national_id,
                'shenase_melli' => $request->shenase_melli,
                'tel_number' => $request->tel_number,
                'mobile_number' => $request->mobile_number,
                'address' => $request->address,
                'edare_kol_id' => $request->edare_kol_id,
                'edare_kol' => $request->edare_kol,
                'edare_shahri_id' => $request->edare_shahri_id,
                'edare_shahri' => $request->edare_shahri,
                'rah_type_id' => $request->rah_type_id,
                'rah_type' => $request->rah_type,
                'name_mehvar_id' => $request->name_mehvar_id,
                'name_mehvar_fa' => $request->name_mehvar_fa,
                'mizan_harim' => $request->mizan_harim,
                'arze_navar' => $request->arze_navar,
                'samt_id' => $request->samt_id,
                'samt' => $request->samt,
                'kilometr' => $request->kilometr,
                'lat' => $request->lat,
                'lon' => $request->lon,
                'zone' => $request->zone,
                'karbari_type_id' => $request->karbari_type_id,
                'karbari_type' => $request->karbari_type,
                'tarh_title' => $request->tarh_title,
                'masahat_zirbana' => $request->masahat_zirbana,
                'ehdasat_type_id' => $request->ehdasat_type_id,
                'ehdasat_type' => $request->ehdasat_type,
                'divarkeshi_distance' => $request->divarkeshi_distance,
                'mostahadesat_distance' => $request->mostahadesat_distance,
                'mahale_ejra_id' => $request->mahale_ejra_id,
                'mahale_ejra' => $request->mahale_ejra,
                'traffic_id' => $request->traffic_id,
                'traffic' => $request->traffic,
                'vaziat_eghtesadi_id' => $request->vaziat_eghtesadi_id,
                'vaziat_eghtesadi' => $request->vaziat_eghtesadi,
                'has_access_id' => $request->has_access_id,
                'has_access' => $request->has_access,
                'shomare_estelam_harim' => $request->shomare_estelam_harim,
                'max_month' => $request->max_month,
                'max_day' => $request->max_day,
                'shomare_tahaodname' => $request->shomare_tahaodname,
                'shomare_daftarkhaneh' => $request->shomare_daftarkhaneh,
                'description' => $request->description,
                'ronevesht' => $request->ronevesht,
            ]);

            return $this->successResponse();
        } catch (\Throwable $th) {
            \Log::error($th->getMessage());
            throw $th;
        }
    }

    public function details(Divarkeshi $divarkeshi): JsonResponse
    {
        return $this->successResponse($divarkeshi);
    }

}
