<?php

namespace App\Controllers;

use App\Core\Controller;
use App\Models\Order;
use App\Models\Cart;

class OrderController extends Controller
{
    private Order $orderModel;
    private Cart $cartModel;

    public function __construct()
    {
        $this->orderModel = new Order();
        $this->cartModel = new Cart();
    }

    public function checkout(): void
    {
        $this->requireAuth();

        if ($this->isAdmin()) {
            $this->redirect('/catalog');
            return;
        }

        $user = $this->getCurrentUser();
        $cartItems = $this->cartModel->getUserCart($user['id']);
        $totals = $this->cartModel->getCartTotal($user['id']);

        $this->view('cart/checkout', [
            'user' => $user,
            'cartItems' => $cartItems,
            'totalQuantity' => $totals['quantity'],
            'totalAmount' => $totals['amount']
        ]);
    }

    public function create(): void
    {
        if (!$this->isAuthenticated()) {
            $this->json([
                'success' => false,
                'message' => 'Требуется авторизация'
            ]);
            return;
        }

        if ($this->isAdmin()) {
            $this->json([
                'success' => false,
                'message' => 'Доступ запрещен'
            ]);
            return;
        }

        $user = $this->getCurrentUser();
        $cartItems = $this->cartModel->getUserCart($user['id']);

        if (empty($cartItems)) {
            $this->json([
                'success' => false,
                'message' => 'Корзина пуста'
            ]);
            return;
        }

        $orderData = [
            'customer_name' => $this->getPost('full_name', $user['full_name']),
            'customer_email' => $this->getPost('email', $user['email']),
            'customer_phone' => $this->getPost('phone', $user['phone']),
            'delivery_address' => $this->getPost('address', ''),
            'delivery_region' => $this->getPost('region', ''),
            'postal_code' => $this->getPost('postal_code', ''),
            'delivery_method' => $this->getPost('delivery', 'courier'),
            'payment_method' => $this->getPost('payment', 'card'),
            'promo_code' => $this->getPost('promo_code', ''),
            'discount' => (float) $this->getPost('discount', 0),
            'delivery_price' => (float) $this->getPost('delivery_price', 2000),
            'notes' => $this->getPost('notes', '')
        ];

        if (empty($orderData['customer_name'])) {
            $this->json([
                'success' => false,
                'message' => 'Укажите ФИО'
            ]);
            return;
        }

        if (empty($orderData['customer_phone'])) {
            $this->json([
                'success' => false,
                'message' => 'Укажите телефон'
            ]);
            return;
        }

        if (empty($orderData['delivery_address'])) {
            $this->json([
                'success' => false,
                'message' => 'Укажите адрес доставки'
            ]);
            return;
        }

        try {
            $result = $this->orderModel->createFromCart($user['id'], $cartItems, $orderData);

            $this->json([
                'success' => true,
                'order_id' => $result['order_id'],
                'order_number' => $result['order_number'],
                'message' => 'Заказ успешно оформлен'
            ]);

        } catch (\Exception $e) {
            $this->json([
                'success' => false,
                'message' => $e->getMessage()
            ]);
        }
    }
}
