<?php

namespace App\Http\Controllers\api\v1;

use App\Http\Requests;
use App\Models\ServiceOrder;
use App\Service\ServiceOrderService;
use App\Traits\JsonResponseTrait;
use Illuminate\Http\Request;

class ServiceOrderController
{
    use JsonResponseTrait;

    protected $serviceOrderService;

    public function __construct(ServiceOrderService $serviceOrderService)
    {
        $this->serviceOrderService = $serviceOrderService;
    }

    /**
     * Get customer orders
     */
    public function customerOrders(Request $request)
    {
        $customer = $request->auth_user;
        
        $orders = $this->serviceOrderService->getCustomerOrders(
            $customer->user_id,
            $request->get('status')
        );

        return $this->successDataResponse(
            data: $orders,
            message: 'Orders retrieved successfully'
        );
    }

    /**
     * Get vendor orders
     */
    public function vendorOrders(Request $request)
    {
        $vendor = $request->auth_user;
        
        $orders = $this->serviceOrderService->getVendorOrders(
            $vendor->user_id,
            $request->get('status')
        );

        return $this->successDataResponse(
            data: $orders,
            message: 'Orders retrieved successfully'
        );
    }

    /**
     * Create a new order
     */
    public function store(Request $request)
    {
        $customer = $request->auth_user;
        
        $validated = $request->validate([
            'vendor_service_id' => 'required|uuid|exists:vendor_services,id',
            'items' => 'required|array|min:1',
            'items.*.price' => 'required|numeric|min:0',
            'items.*.quantity' => 'required|integer|min:1',
            'delivery_address' => 'nullable|string|max:500',
            'customer_notes' => 'nullable|string|max:1000',
        ]);

        $order = $this->serviceOrderService->createOrder(
            $customer,
            $validated['vendor_service_id'],
            $validated['items'],
            $validated['delivery_address'] ?? null,
            $validated['customer_notes'] ?? null
        );

        return $this->successDataResponse(
            data: $order,
            message: 'Order created successfully'
        );
    }

    /**
     * Update order status
     */
    public function updateStatus($orderId, Request $request)
    {
        $order = ServiceOrder::findOrFail($orderId);
        
        $validated = $request->validate([
            'status' => 'required|in:confirmed,in_progress,completed,cancelled,refunded',
            'vendor_notes' => 'nullable|string|max:1000',
        ]);

        $this->serviceOrderService->updateOrderStatus($order, $validated['status'], $validated['vendor_notes'] ?? null);

        return $this->successResponse(message: 'Order status updated successfully');
    }

    /**
     * Confirm payment
     */
    public function confirmPayment($orderId)
    {
        $order = ServiceOrder::findOrFail($orderId);
        
        $this->serviceOrderService->confirmPayment($order);

        return $this->successResponse(message: 'Payment confirmed successfully');
    }

    /**
     * Confirm delivery
     */
    public function confirmDelivery($orderId)
    {
        $order = ServiceOrder::findOrFail($orderId);
        
        $this->serviceOrderService->confirmDelivery($order);

        return $this->successResponse(message: 'Delivery confirmed successfully');
    }

    /**
     * Cancel order
     */
    public function cancel($orderId, Request $request)
    {
        $order = ServiceOrder::findOrFail($orderId);
        
        $reason = $request->input('reason');
        
        $this->serviceOrderService->cancelOrder($order, $reason);

        return $this->successResponse(message: 'Order cancelled successfully');
    }

    /**
     * Refund order
     */
    public function refund($orderId, Request $request)
    {
        $order = ServiceOrder::findOrFail($orderId);
        
        $reason = $request->input('reason');
        
        $this->serviceOrderService->refundOrder($order, $reason);

        return $this->successResponse(message: 'Order refunded successfully');
    }
}
