<?php
namespace App\Models;
use App\Core\Model;
class Order extends Model
{
    protected $table = 'orders';

    public function recent($limit = 5)
    {
        $stmt = $this->conn->prepare("SELECT * FROM orders ORDER BY id DESC LIMIT :limit");
        $stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function countAll()
    {
        $stmt = $this->conn->query("SELECT COUNT(*) as total FROM orders");
        return $stmt->fetch()['total'];
    }

    public function countRecent($days = 7)
    {
        $stmt = $this->conn->prepare("SELECT COUNT(*) as total FROM orders WHERE created_at >= DATE_SUB(NOW(), INTERVAL :days DAY)");
        $stmt->bindValue(':days', $days, \PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetch()['total'];
    }

    public function getLastOrderDate()
    {
        $stmt = $this->conn->query("SELECT created_at FROM orders ORDER BY id DESC LIMIT 1");
        $result = $stmt->fetch();
        return $result ? $result['created_at'] : null;
    }
}
