<?php

namespace App\Http\Controllers\Api\V1\Inventory;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Inventory\StorePurchaseOrderRequest;
use App\Http\Requests\Api\V1\Inventory\UpdatePurchaseOrderRequest;
use App\Http\Requests\Api\V1\Inventory\ReceivePurchaseOrderRequest;
use App\Services\PurchaseOrderService;
use App\Services\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PurchaseOrderController extends Controller
{
    public function __construct(
        protected PurchaseOrderService $purchaseOrderService,
        protected ResponseService $responseService
    ) {}

    public function index(): JsonResponse
    {
        $purchaseOrders = $this->purchaseOrderService->getAll();
        return $this->responseService->success($purchaseOrders, 'Purchase orders retrieved successfully');
    }

    public function forBranch(int $branchId): JsonResponse
    {
        $purchaseOrders = $this->purchaseOrderService->getForBranch($branchId);
        return $this->responseService->success($purchaseOrders, 'Purchase orders retrieved successfully');
    }

    public function byStatus(int $branchId, string $status): JsonResponse
    {
        $validStatuses = ['draft', 'pending', 'approved', 'received', 'cancelled'];
        if (!in_array($status, $validStatuses)) {
            return $this->responseService->error('Invalid status', 422);
        }

        $purchaseOrders = $this->purchaseOrderService->getByStatus($branchId, $status);
        return $this->responseService->success($purchaseOrders, 'Purchase orders retrieved successfully');
    }

    public function pending(int $branchId): JsonResponse
    {
        $purchaseOrders = $this->purchaseOrderService->getPending($branchId);
        return $this->responseService->success($purchaseOrders, 'Pending purchase orders retrieved successfully');
    }

    public function store(StorePurchaseOrderRequest $request): JsonResponse
    {
        $data = $request->validated();
        $data['user_id'] = auth()->id();

        $purchaseOrder = $this->purchaseOrderService->create($data);
        return $this->responseService->success($purchaseOrder, 'Purchase order created successfully', 201);
    }

    public function show(int $id): JsonResponse
    {
        $purchaseOrder = $this->purchaseOrderService->find($id);

        if (!$purchaseOrder) {
            return $this->responseService->error('Purchase order not found', 404);
        }

        return $this->responseService->success($purchaseOrder, 'Purchase order retrieved successfully');
    }

    public function update(UpdatePurchaseOrderRequest $request, int $id): JsonResponse
    {
        $purchaseOrder = $this->purchaseOrderService->find($id);

        if (!$purchaseOrder) {
            return $this->responseService->error('Purchase order not found', 404);
        }

        $purchaseOrder = $this->purchaseOrderService->update($id, $request->validated());
        return $this->responseService->success($purchaseOrder, 'Purchase order updated successfully');
    }

    public function destroy(int $id): JsonResponse
    {
        $purchaseOrder = $this->purchaseOrderService->find($id);

        if (!$purchaseOrder) {
            return $this->responseService->error('Purchase order not found', 404);
        }

        $this->purchaseOrderService->delete($id);
        return $this->responseService->success(null, 'Purchase order deleted successfully');
    }

    public function updateStatus(Request $request, int $id): JsonResponse
    {
        $request->validate([
            'status' => 'required|in:draft,pending,approved,received,cancelled',
        ]);

        $purchaseOrder = $this->purchaseOrderService->updateStatus($id, $request->status);

        if (!$purchaseOrder) {
            return $this->responseService->error('Purchase order not found', 404);
        }

        return $this->responseService->success($purchaseOrder, 'Purchase order status updated successfully');
    }

    public function receive(ReceivePurchaseOrderRequest $request, int $id): JsonResponse
    {
        $purchaseOrder = $this->purchaseOrderService->receive(
            $id,
            $request->items,
            auth()->id()
        );

        if (!$purchaseOrder) {
            return $this->responseService->error('Purchase order not found or cannot be received', 422);
        }

        return $this->responseService->success($purchaseOrder, 'Purchase order received successfully');
    }

    public function cancel(int $id): JsonResponse
    {
        $purchaseOrder = $this->purchaseOrderService->find($id);

        if (!$purchaseOrder) {
            return $this->responseService->error('Purchase order not found', 404);
        }

        $purchaseOrder = $this->purchaseOrderService->cancel($id);
        return $this->responseService->success($purchaseOrder, 'Purchase order cancelled successfully');
    }
}
