<?php
namespace Models;

class Vehicle {
    private $db;
    
    public function __construct($db) {
        $this->db = $db;
    }
    
    public function findByCompany($companyId) {
        return $this->db->exec(
            'SELECT v.*, c.name as customer_name, c.phone as customer_phone 
             FROM vehicles v 
             INNER JOIN customers c ON v.customer_id = c.id 
             WHERE c.company_id = ? 
             ORDER BY v.registration_number',
            [$companyId]
        );
    }
    
    public function findById($id) {
        $result = $this->db->exec(
            'SELECT v.*, c.name as customer_name, c.phone as customer_phone 
             FROM vehicles v 
             INNER JOIN customers c ON v.customer_id = c.id 
             WHERE v.id = ?',
            [$id]
        );
        return $result ? $result[0] : null;
    }
    
    public function findByRegistration($registration) {
        $result = $this->db->exec(
            'SELECT v.*, c.name as customer_name, c.phone as customer_phone, c.email as customer_email 
             FROM vehicles v 
             INNER JOIN customers c ON v.customer_id = c.id 
             WHERE UPPER(v.registration_number) = UPPER(?)',
            [$registration]
        );
        return $result ? $result[0] : null;
    }
    
    public function findByCustomer($customerId) {
        return $this->db->exec('SELECT * FROM vehicles WHERE customer_id = ? ORDER BY registration_number', [$customerId]);
    }
    
    public function create($data) {
        $this->db->exec(
            'INSERT INTO vehicles (customer_id, registration_number, make, model, year, vin) VALUES (?, ?, ?, ?, ?, ?)',
            [$data['customer_id'], strtoupper($data['registration_number']), $data['make'] ?? null, $data['model'] ?? null, $data['year'] ?? null, $data['vin'] ?? null]
        );
        return $this->db->lastinsertid();
    }
    
    public function update($id, $data) {
        $sets = [];
        $params = [];
        
        foreach (['customer_id', 'registration_number', 'make', 'model', 'year', 'vin'] as $field) {
            if (isset($data[$field])) {
                $value = $field === 'registration_number' ? strtoupper($data[$field]) : $data[$field];
                $sets[] = "$field = ?";
                $params[] = $value;
            }
        }
        
        $sets[] = "updated_at = CURRENT_TIMESTAMP";
        $params[] = $id;
        
        $this->db->exec('UPDATE vehicles SET ' . implode(', ', $sets) . ' WHERE id = ?', $params);
    }
    
    public function delete($id) {
        $this->db->exec('DELETE FROM vehicles WHERE id = ?', [$id]);
    }
    
    public function countByCompany($companyId) {
        $result = $this->db->exec(
            'SELECT COUNT(*) as count FROM vehicles v INNER JOIN customers c ON v.customer_id = c.id WHERE c.company_id = ?',
            [$companyId]
        );
        return $result[0]['count'];
    }
    
    public function countAll() {
        $result = $this->db->exec('SELECT COUNT(*) as count FROM vehicles');
        return $result[0]['count'];
    }
}



