<?php

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

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

class AzmayeshTypeController extends Controller
{
    use ApiResponse;

    public function index(Request $request): JsonResponse
    {
        $columns = array(
            'id', 'name', 'updated_at', 'created_at'
        );

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

        $query = AzmayeshType::query();

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

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

    public function list(): JsonResponse
    {
        $allTypes = AzmayeshType::all();

        return $this->successResponse($allTypes);
    }

    public function fields(AzmayeshType $azmayeshType): JsonResponse
    {
        return $this->successResponse($azmayeshType->azmayeshFields);
    }

    public function store(StoreRequest $request): JsonResponse
    {
        DB::transaction(function () use ($request)
        {
            $azmayeshType = AzmayeshType::query()->create([
                'name' => $request->name
            ]);

            $fieldsID = [];

            foreach ($request->fields as $field)
            {
                $azmayeshField = AzmayeshField::query()->firstOrCreate($field, $field);
                $fieldsID[] = $azmayeshField->id;
            }

            $azmayeshType->azmayeshFields()->attach($fieldsID);

            auth()->user()->addActivityComplete(1162);
        });

        return $this->successResponse();
    }

    /**
     * Display the specified resource.
     */
    public function show(AzmayeshType $azmayeshType): JsonResponse
    {
        return $this->successResponse($azmayeshType->load(['azmayeshFields']));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateRequest $request, AzmayeshType $azmayeshType): JsonResponse
    {
        DB::transaction(function () use ($request, $azmayeshType)
        {
            $azmayeshType->update([
                'name' => $request->name
            ]);

            $fieldsID = [];

            foreach ($request->fields as $field)
            {
                $azmayeshField = AzmayeshField::query()->updateOrCreate($field, $field);
                $fieldsID[] = $azmayeshField->id;
            }

            $azmayeshType->azmayeshFields()->sync($fieldsID);

            auth()->user()->addActivityComplete(1163);
        });

        return $this->successResponse();
    }

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

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

            return $this->successResponse();
        }
        catch (QueryException $exception) {
            return $this->errorResponse('امکان حذف وجود ندارد. ابتدا آزمایش های مرتبط با این نوع را حذف نمایید');
        }
    }
}
