<?php

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

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

class AzmayeshSampleController extends Controller
{
    use ApiResponse;
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request, string $azmayesh_id): JsonResponse
    {
        $columns = array(
            'id', 'updated_at', 'created_at'
        );

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

        $query = AzmayeshSample::query()->where('azmayesh_id', '=', $azmayesh_id);

        $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
    {
        $azmayesh = Azmayesh::query()->findOrFail($request->azmayesh_id);

        AzmayeshSample::query()->create([
            'azmayesh_id' => $azmayesh->id,
            'azmayesh_project_name' => $azmayesh->project_name,
            'data' => $request->data,
        ]);

        return $this->successResponse();
    }

    /**
     * Display the specified resource.
     */
    public function show(AzmayeshSample $azmayeshSample): JsonResponse
    {
        return $this->successResponse($azmayeshSample);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateRequest $request, AzmayeshSample $azmayeshSample): JsonResponse
    {
        $azmayesh = Azmayesh::query()->findOrFail($request->azmayesh_id);

        $azmayeshSample->update([
            'azmayesh_id' => $azmayesh->id,
            'azmayesh_project_name' => $azmayesh->project_name,
            'data' => $request->data,
        ]);

        return $this->successResponse();
    }

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

        return $this->successResponse();
    }
}
