<?php
require_once __DIR__ . '/../config/Database.php';

class Customer {
    private $db;
    private $conn;
    
    public function __construct() {
        $this->db = new Database();
        $this->conn = $this->db->getConnection();
    }
    
    public function getAllCustomers() {
        $query = "SELECT * FROM customers WHERE status = 'active' ORDER BY name";
        $result = $this->conn->query($query);
        return $result->fetch_all(MYSQLI_ASSOC);
    }
    
    public function getCustomer($customerId) {
        $query = "SELECT * FROM customers WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bind_param("i", $customerId);
        $stmt->execute();
        return $stmt->get_result()->fetch_assoc();
    }
    
    public function createCustomer($data) {
        $query = "INSERT INTO customers (name, email, phone, address, city, postal_code, birthday) 
                  VALUES (?, ?, ?, ?, ?, ?, ?)";
        $stmt = $this->conn->prepare($query);
        $stmt->bind_param("sssssss", $data['name'], $data['email'], $data['phone'], 
                          $data['address'], $data['city'], $data['postal_code'], $data['birthday']);
        if ($stmt->execute()) {
            return ['success' => true, 'id' => $this->conn->insert_id];
        }
        return ['success' => false];
    }
    
    public function updateCustomer($customerId, $data) {
        $query = "UPDATE customers SET name=?, email=?, phone=?, address=?, city=?, postal_code=?, birthday=? WHERE id=?";
        $stmt = $this->conn->prepare($query);
        $stmt->bind_param("sssssssi", $data['name'], $data['email'], $data['phone'], 
                          $data['address'], $data['city'], $data['postal_code'], $data['birthday'], $customerId);
        return $stmt->execute();
    }
    
    public function getUpcomingBirthdays($days = 3) {
        $fromDate = date('m-d');
        $toDate = date('m-d', strtotime("+{$days} days"));
        
        $query = "SELECT * FROM customers 
                  WHERE status = 'active' AND DATE_FORMAT(birthday, '%m-%d') BETWEEN ? AND ? 
                  ORDER BY DATE_FORMAT(birthday, '%m-%d')";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bind_param("ss", $fromDate, $toDate);
        $stmt->execute();
        return $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    }
    
    public function searchCustomers($searchTerm) {
        $searchTerm = "%" . $this->db->escape($searchTerm) . "%";
        $query = "SELECT * FROM customers 
                  WHERE status = 'active' AND (name LIKE ? OR email LIKE ? OR phone LIKE ?)
                  ORDER BY name LIMIT 20";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bind_param("sss", $searchTerm, $searchTerm, $searchTerm);
        $stmt->execute();
        return $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    }
    
    public function getCustomerStats($customerId) {
        $query = "SELECT 
                    COUNT(DISTINCT id) as total_purchases,
                    SUM(final_amount) as total_spent,
                    MAX(transaction_date) as last_purchase,
                    AVG(final_amount) as avg_purchase_value
                  FROM transactions 
                  WHERE customer_id = ? AND status != 'cancelled'";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bind_param("i", $customerId);
        $stmt->execute();
        return $stmt->get_result()->fetch_assoc();
    }
}
?>
