<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Quality\QualityDocumentStoreRequest;
use App\Http\Requests\Quality\QualityDocumentUpdateRequest;
use App\Services\QualityDocumentService;
use Illuminate\Http\Request;

class QualityDocumentController extends Controller
{
    public function __construct(private QualityDocumentService $service) {}

    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->index($request->all());
        }, __('message.fetched'));
    }

    public function stats(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->stats($request->all());
        }, __('message.fetched'));
    }

    public function calendar(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->calendar($request->all());
        }, __('message.fetched'));
    }

    public function availableOrders(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->availableOrders($request->all());
        }, __('message.fetched'));
    }

    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.fetched'));
    }

    public function store(QualityDocumentStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.created'));
    }

    public function update(QualityDocumentUpdateRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->update($id, $request->validated());
        }, __('message.updated'));
    }

    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $this->service->destroy($id);

            return 'success';
        }, __('message.deleted'));
    }

    public function complete(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->complete($id);
        }, __('message.updated'));
    }

    public function attachOrders(Request $request, int $id)
    {
        $request->validate([
            'order_ids' => ['required', 'array', 'min:1'],
            'order_ids.*' => ['required', 'integer'],
        ]);

        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->attachOrders($id, $request->input('order_ids'));
        }, __('message.updated'));
    }

    public function detachOrder(int $id, int $orderId)
    {
        return ApiResponse::handle(function () use ($id, $orderId) {
            return $this->service->detachOrder($id, $orderId);
        }, __('message.updated'));
    }

    public function inspectLocation(Request $request, int $id, int $locId)
    {
        $request->validate([
            'post_width' => ['nullable', 'integer'],
            'post_height' => ['nullable', 'integer'],
            'change_reason' => ['nullable', 'string', 'max:500'],
            'inspection_status' => ['nullable', 'string', 'in:pending,completed'],
        ]);

        return ApiResponse::handle(function () use ($request, $id, $locId) {
            return $this->service->inspectLocation($id, $locId, $request->all());
        }, __('message.updated'));
    }

    public function requestDocument(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->requestDocument($id);
        }, __('message.fetched'));
    }

    public function resultDocument(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->resultDocument($id);
        }, __('message.fetched'));
    }
}
