<?php
require_once 'Database.php';

class Admin {
    private $db;

    public function __construct() {
        $this->db = new Database();
    }

    public function getStats() {
        $stats = [];
        
        // Total users
        $stmt = $this->db->query("SELECT COUNT(*) as total FROM users");
        $stats['total_users'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

        // Users by role
        $stmt = $this->db->query("SELECT role, COUNT(*) as count FROM users GROUP BY role");
        $stats['users_by_role'] = $stmt->fetchAll(PDO::FETCH_ASSOC);

        // Total shops
        $stmt = $this->db->query("SELECT COUNT(*) as total FROM shops WHERE is_active = TRUE");
        $stats['total_shops'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

        return $stats;
    }

    public function getAllUsers() {
        $stmt = $this->db->query(
            "SELECT id, email, name, role, phone, created_at 
             FROM users 
             ORDER BY created_at DESC"
        );

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function getAllDeliveries() {
        $stmt = $this->db->query(
            "SELECT d.*, u.name as customer_name, r.name as rider_name
             FROM deliveries d
             LEFT JOIN users u ON d.user_id = u.id
             LEFT JOIN users r ON d.rider_id = r.id
             ORDER BY d.created_at DESC"
        );

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function getAllShops() {
        $stmt = $this->db->query(
            "SELECT s.*, u.name as owner_name
             FROM shops s
             JOIN users u ON s.owner_id = u.id
             ORDER BY s.created_at DESC"
        );

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function getSlides() {
        $stmt = $this->db->query(
            "SELECT * FROM slides WHERE is_active = TRUE ORDER BY created_at DESC"
        );

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function addSlide($data) {
        $stmt = $this->db->query(
            "INSERT INTO slides (title, description, image_path) VALUES (?, ?, ?)",
            [$data['title'], $data['description'], $data['image_path']]
        );

        return $this->db->lastInsertId();
    }

    public function updateSlide($slide_id, $data) {
        $stmt = $this->db->query(
            "UPDATE slides SET title = ?, description = ?, image_path = ? WHERE id = ?",
            [$data['title'], $data['description'], $data['image_path'], $slide_id]
        );

        return $stmt->rowCount() > 0;
    }

    public function deleteSlide($slide_id) {
        $stmt = $this->db->query("DELETE FROM slides WHERE id = ?", [$slide_id]);
        return $stmt->rowCount() > 0;
    }

    public function updateUserRole($user_id, $role) {
        $stmt = $this->db->query(
            "UPDATE users SET role = ? WHERE id = ?",
            [$role, $user_id]
        );

        return $stmt->rowCount() > 0;
    }

    public function toggleShopStatus($shop_id) {
        $stmt = $this->db->query(
            "UPDATE shops SET is_active = NOT is_active WHERE id = ?",
            [$shop_id]
        );

        return $stmt->rowCount() > 0;
    }
}
?>