<?php

namespace App\Http\Controllers\V3;

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

class DamageManagementController extends Controller
{
    use ApiResponse;
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request): JsonResponse
    {
        $allowedFilters = ['*'];
        $allowedSortings = ['*'];

        $query = Damage::query();

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


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

    public function list(): JsonResponse
    {
        $damages = Damage::query()->where('status', '=', 1)->get();
        return $this->successResponse($damages);
    }


    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreRequest $request): JsonResponse
    {
        Damage::query()->create([
            'title' => $request->title,
            'unit' => $request->unit,
            'base_price' => $request->base_price,
            'status' => 1,
        ]);

        return $this->successResponse();
    }

    /**
     * Display the specified resource.
     */
    public function show(Damage $damage): JsonResponse

    {
        return $this->successResponse($damage);
    }

    /**
     * Show the form for editing the specified resource.
     */

    public function update(UpdateRequest $request, Damage $damage): JsonResponse

    {
        $damage->update([
            'title' => $request->title,
            'unit' => $request->unit,
            'base_price' => $request->base_price,
        ]);

        return $this->successResponse();
    }

    /**
     * Remove the specified resource from storage.
     */
    public function activate(Damage $damage): JsonResponse
    {
        $damage->status == 1 ? $damage->update(['status' => 0]) : $damage->update(['status' => 1]);

        return $this->successResponse();
    }
}
