<?php
// app/Models/Violation.php
class Violation {

    public static function create(array $d): int {
        $s = db()->prepare(
            'INSERT INTO violations(officer_id,vehicle_id,plate_number,violation_type,amount,notes)
             VALUES(?,?,?,?,?,?)'
        );
        $s->execute([
            $d['officer_id'], $d['vehicle_id'], $d['plate_number'],
            $d['violation_type'], $d['amount'], $d['notes'] ?? null
        ]);
        return (int)db()->lastInsertId();
    }

    public static function find(int $id): ?array {
        $s = db()->prepare(
            'SELECT v.*,
                    u.name  AS officer_name,
                    veh.brand, veh.model
             FROM violations v
             JOIN users    u   ON u.id   = v.officer_id
             JOIN vehicles veh ON veh.id = v.vehicle_id
             WHERE v.id = ?'
        );
        $s->execute([$id]);
        return $s->fetch() ?: null;
    }

    public static function byVehicle(int $vehicleId): array {
        $s = db()->prepare(
            'SELECT v.*, u.name AS officer_name
             FROM violations v
             JOIN users u ON u.id = v.officer_id
             WHERE v.vehicle_id = ?
             ORDER BY v.issued_at DESC'
        );
        $s->execute([$vehicleId]);
        return $s->fetchAll();
    }

    public static function byOfficer(int $officerId, int $limit = 50): array {
        $s = db()->prepare(
            'SELECT v.*, veh.brand, veh.model
             FROM violations v
             JOIN vehicles veh ON veh.id = v.vehicle_id
             WHERE v.officer_id = ?
             ORDER BY v.issued_at DESC
             LIMIT ' . (int)$limit
        );
        $s->execute([$officerId]);
        return $s->fetchAll();
    }

    /** كل المخالفات غير المدفوعة لسيارة معينة */
    public static function unpaidByVehicle(int $vehicleId): array {
        $s = db()->prepare(
            "SELECT * FROM violations WHERE vehicle_id=? AND status='unpaid' ORDER BY issued_at DESC"
        );
        $s->execute([$vehicleId]);
        return $s->fetchAll();
    }

    public static function markPaid(int $id, int $paidBy, string $method, ?string $ref): void {
        db()->prepare(
            "UPDATE violations SET status='paid', paid_at=NOW() WHERE id=?"
        )->execute([$id]);

        // سجل الدفع
        $amount = (float)(db()->query("SELECT amount FROM violations WHERE id=$id")->fetchColumn());
        db()->prepare(
            'INSERT INTO payment_logs(violation_id,paid_by,amount,payment_method,reference)
             VALUES(?,?,?,?,?)'
        )->execute([$id, $paidBy, $amount, $method, $ref]);
    }

    public static function cancel(int $id): void {
        db()->prepare("UPDATE violations SET status='cancelled' WHERE id=?")->execute([$id]);
    }

    public static function countUnpaidByVehicle(int $vehicleId): int {
        $s = db()->prepare("SELECT COUNT(*) FROM violations WHERE vehicle_id=? AND status='unpaid'");
        $s->execute([$vehicleId]);
        return (int)$s->fetchColumn();
    }
}
