<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Order\CreateFromQuoteRequest;
use App\Http\Requests\Order\CreateProductionOrderRequest;
use App\Http\Requests\Order\OrderBulkDeleteRequest;
use App\Http\Requests\Order\StoreOrderRequest;
use App\Http\Requests\Order\UpdateOrderRequest;
use App\Http\Requests\Order\UpdateOrderStatusRequest;
use App\Services\OrderService;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function __construct(private OrderService $service) {}

    /**
     * 목록 조회
     */
    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->index($request->all());
        }, __('message.order.fetched'));
    }

    /**
     * 통계 조회
     */
    public function stats()
    {
        return ApiResponse::handle(function () {
            return $this->service->stats();
        }, __('message.order.fetched'));
    }

    /**
     * 단건 조회
     */
    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.order.fetched'));
    }

    /**
     * 생성
     */
    public function store(StoreOrderRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.order.created'));
    }

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

    /**
     * 일괄 삭제
     */
    public function bulkDestroy(OrderBulkDeleteRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $validated = $request->validated();

            return $this->service->bulkDestroy(
                $validated['ids'],
                $validated['force'] ?? false
            );
        }, __('message.order.bulk_deleted'));
    }

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

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

    /**
     * 상태 변경
     */
    public function updateStatus(UpdateOrderStatusRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->updateStatus($id, $request->validated()['status']);
        }, __('message.order.status_updated'));
    }

    /**
     * 견적에서 수주 생성
     */
    public function createFromQuote(CreateFromQuoteRequest $request, int $quoteId)
    {
        return ApiResponse::handle(function () use ($request, $quoteId) {
            return $this->service->createFromQuote($quoteId, $request->validated());
        }, __('message.order.created_from_quote'));
    }

    /**
     * 생산지시 생성
     */
    public function createProductionOrder(CreateProductionOrderRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->createProductionOrder($id, $request->validated());
        }, __('message.order.production_order_created'));
    }

    /**
     * 수주확정 되돌리기 (수주등록 상태로 변경)
     */
    public function revertOrderConfirmation(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->revertOrderConfirmation($id);
        }, __('message.order.order_confirmation_reverted'));
    }

    /**
     * 절곡 BOM 품목 재고 확인
     */
    public function checkBendingStock(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->checkBendingStockForOrder($id);
        }, __('message.fetched'));
    }

    /**
     * 생산지시 되돌리기 (작업지시 및 관련 데이터 삭제)
     */
    public function revertProductionOrder(Request $request, int $id)
    {
        $force = $request->boolean('force', false);
        $reason = $request->input('reason');

        return ApiResponse::handle(function () use ($id, $force, $reason) {
            return $this->service->revertProductionOrder($id, $force, $reason);
        }, __('message.order.production_order_reverted'));
    }
}
