<?php

namespace App\Http\Controllers\Admin\Manage\Cutting;

use App\Enums\CuttingStatus;
use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\CuttingRequest;
use App\Http\Requests\Admin\Manage\CuttingStatusTransitionRequest;
use App\Models\Cutting;
use App\Services\Manage\CuttingService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
use Inertia\Response;

class CuttingController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly CuttingService $cuttingService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);

        return Inertia::render('admin/manage/cuttings/Index', [
            'cuttings' => $this->cuttingService->paginateForIndex($tableQuery, $request->user()),
            'inProgressCuttings' => $this->cuttingService->getInProgressCuttings($request->user()),
            'filters' => $this->dataTableFilters($tableQuery),
        ]);
    }

    public function create(Request $request): Response
    {
        $user = $request->user();

        return Inertia::render('admin/manage/cuttings/Create', [
            'rawMaterialCatalog' => $this->cuttingService->rawMaterialCatalog(user: $user),
            'productCatalog' => $this->cuttingService->productCatalog(user: $user),
            'draftMaterials' => $this->cuttingService->draftMaterialsForUser($user),
            'draftResults' => $this->cuttingService->draftResultsForUser($user),
        ]);
    }

    public function store(CuttingRequest $request): RedirectResponse
    {
        $this->cuttingService->create($request->validated(), $request->user());

        $this->flashCreated('Proses cutting');

        return redirect()->route('admin.manage.cuttings.index');
    }

    public function edit(Cutting $cutting): Response|RedirectResponse
    {
        try {
            $cutting->ensureEditable();
        } catch (ValidationException $exception) {
            $this->flashError($exception->validator->errors()->first('status'));

            return redirect()->route('admin.manage.cuttings.index');
        }

        return Inertia::render('admin/manage/cuttings/Edit', [
            'cutting' => $this->cuttingService->findForEdit($cutting),
            'rawMaterialCatalog' => $this->cuttingService->rawMaterialCatalog($cutting),
            'productCatalog' => $this->cuttingService->productCatalog($cutting),
        ]);
    }

    public function update(CuttingRequest $request, Cutting $cutting): RedirectResponse
    {
        $this->cuttingService->update($cutting, $request->validated());

        $this->flashUpdated('Proses cutting');

        return redirect()->route('admin.manage.cuttings.index');
    }

    public function destroy(Cutting $cutting): RedirectResponse
    {
        $this->cuttingService->delete($cutting);

        $this->flashDeleted('Proses cutting');

        return redirect()->route('admin.manage.cuttings.index');
    }

    public function transitionStatus(CuttingStatusTransitionRequest $request, Cutting $cutting): RedirectResponse
    {
        $status = CuttingStatus::from($request->validated('status'));

        $this->cuttingService->transitionStatus(
            $cutting,
            $status,
            $request->user(),
            $request->validated('reason'),
            $request->validated('verification_note'),
            $request->validated('results'),
            $request->validated('result_prices'),
        );

        $this->flashSuccess($status->transitionStatusMessage());

        return redirect()->route('admin.manage.cuttings.index');
    }
}
