<?php

namespace App\Models;

use App\Core\Model;

class Cart extends Model
{
    protected string $table = 'cart';
    protected string $primaryKey = 'cart_id';

    public function getUserCart(int $userId): array
    {
        $sql = "SELECT 
                    c.cart_id,
                    c.product_id,
                    c.quantity,
                    p.name,
                    p.price,
                    p.image_url,
                    p.stock_quantity,
                    p.is_available
                FROM {$this->table} c
                JOIN products p ON c.product_id = p.product_id
                WHERE c.user_id = ? AND p.is_available = TRUE
                ORDER BY c.created_at DESC";
        return $this->query($sql, [$userId]);
    }

    public function getCartTotal(int $userId): array
    {
        $sql = "SELECT 
                    SUM(c.quantity) as total_quantity,
                    SUM(c.quantity * p.price) as total_amount
                FROM {$this->table} c
                JOIN products p ON c.product_id = p.product_id
                WHERE c.user_id = ? AND p.is_available = TRUE";
        $result = $this->queryOne($sql, [$userId]);
        
        return [
            'quantity' => (int) ($result['total_quantity'] ?? 0),
            'amount' => (float) ($result['total_amount'] ?? 0)
        ];
    }

    public function getCount(int $userId): int
    {
        $sql = "SELECT COALESCE(SUM(quantity), 0) as total FROM {$this->table} WHERE user_id = ?";
        $result = $this->queryOne($sql, [$userId]);
        return (int) $result['total'];
    }

    public function addItem(int $userId, int $productId, int $quantity = 1): bool
    {
        $existing = $this->findWhere([
            'user_id' => $userId,
            'product_id' => $productId
        ]);

        if ($existing) {
            $newQuantity = $existing['quantity'] + $quantity;
            return $this->update($existing['cart_id'], [
                'quantity' => $newQuantity,
                'updated_at' => date('Y-m-d H:i:s')
            ]);
        }

        return $this->create([
            'user_id' => $userId,
            'product_id' => $productId,
            'quantity' => $quantity
        ]) !== null;
    }

    public function updateQuantity(int $userId, int $productId, int $quantity): bool
    {
        $sql = "UPDATE {$this->table}
                SET quantity = ?, updated_at = CURRENT_TIMESTAMP
                WHERE user_id = ? AND product_id = ?";
        return $this->execute($sql, [$quantity, $userId, $productId]);
    }

    public function removeItem(int $userId, int $productId): bool
    {
        $sql = "DELETE FROM {$this->table} WHERE user_id = ? AND product_id = ?";
        return $this->execute($sql, [$userId, $productId]);
    }

    public function clearCart(int $userId): bool
    {
        $sql = "DELETE FROM {$this->table} WHERE user_id = ?";
        return $this->execute($sql, [$userId]);
    }

    public function hasItem(int $userId, int $productId): bool
    {
        $item = $this->findWhere([
            'user_id' => $userId,
            'product_id' => $productId
        ]);
        return $item !== null;
    }

    public function getItem(int $userId, int $productId): ?array
    {
        return $this->findWhere([
            'user_id' => $userId,
            'product_id' => $productId
        ]);
    }
}
