<?php
declare(strict_types=1);

namespace Controllers;

use Helpers\Auth;
use Helpers\Request;
use Helpers\Response;
use Services\OrderService;
use Validators\Validator;

final class OrderController
{
    public function cod(): never
    {
        try { Auth::requireUser(); } catch (\Throwable $e) { Response::error('Authentication required before checkout', 401); }
        $data = Request::input();
        $errors = Validator::validate($data, [
            'customer_name' => 'required|min:2', 'customer_phone' => 'required|min:10',
            'address_line1' => 'required', 'city' => 'required', 'state' => 'required', 'pincode' => 'required', 'items' => 'required|array'
        ]);
        if ($errors) Response::error('Validation failed', 422, $errors);
        try { Response::success((new OrderService())->createCod($data), 'COD order placed', 201); }
        catch (\Throwable $e) { Response::error($e->getMessage(), 400); }
    }
}
