<?php
/**
 * Ligne Controller
 * Handles bus ligne (route) management
 */

class LigneController {
    private $db;
    private $ligneModel;

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

    /**
     * Get all lignes
     */
    public function getAll() {
        Auth::requireAuth();

        $filters = [];

        if (isset($_GET['is_active'])) {
            $filters['is_active'] = (int)$_GET['is_active'];
        }

        if (isset($_GET['name'])) {
            $filters['name'] = $_GET['name'];
        }

        if (isset($_GET['start_point'])) {
            $filters['start_point'] = $_GET['start_point'];
        }

        if (isset($_GET['end_point'])) {
            $filters['end_point'] = $_GET['end_point'];
        }

        $lignes = $this->ligneModel->getAll($filters);

        Auth::sendSuccess($lignes, 'Lignes retrieved successfully');
    }

    /**
     * Get ligne by ID
     */
    public function getById($id) {
        Auth::requireAuth();

        $ligne = $this->ligneModel->getById($id);

        if ($ligne) {
            Auth::sendSuccess($ligne, 'Ligne retrieved successfully');
        } else {
            Auth::sendError('Ligne not found', 404);
        }
    }

    /**
     * Create new ligne (Admin only)
     */
    public function create() {
        Auth::requireAdmin();

        $data = Auth::getRequestBody();

        // Validate required fields
        $requiredFields = ['ligne_code', 'name', 'start_point', 'end_point'];
        foreach ($requiredFields as $field) {
            if (!isset($data[$field]) || empty($data[$field])) {
                Auth::sendError("Field '$field' is required", 400);
            }
        }

        // Check if ligne code already exists
        if ($this->ligneModel->codeExists($data['ligne_code'])) {
            Auth::sendError('Ligne code already exists', 409);
        }

        $ligneId = $this->ligneModel->create($data);

        if ($ligneId) {
            $ligne = $this->ligneModel->getById($ligneId);
            Auth::sendSuccess($ligne, 'Ligne created successfully', 201);
        } else {
            Auth::sendError('Failed to create ligne', 500);
        }
    }

    /**
     * Update ligne (Admin only)
     */
    public function update($id) {
        Auth::requireAdmin();

        $data = Auth::getRequestBody();

        if (empty($data)) {
            Auth::sendError('No data provided for update', 400);
        }

        // Check if ligne exists
        $existingLigne = $this->ligneModel->getById($id);
        if (!$existingLigne) {
            Auth::sendError('Ligne not found', 404);
        }

        // Check ligne code uniqueness if being updated
        if (isset($data['ligne_code']) && $data['ligne_code'] !== $existingLigne['ligne_code']) {
            if ($this->ligneModel->codeExists($data['ligne_code'], $id)) {
                Auth::sendError('Ligne code already exists', 409);
            }
        }

        $success = $this->ligneModel->update($id, $data);

        if ($success) {
            $updatedLigne = $this->ligneModel->getById($id);
            Auth::sendSuccess($updatedLigne, 'Ligne updated successfully');
        } else {
            Auth::sendError('Failed to update ligne', 500);
        }
    }

    /**
     * Delete ligne (Admin only)
     */
    public function delete($id) {
        Auth::requireAdmin();

        // Check if ligne exists
        $ligne = $this->ligneModel->getById($id);
        if (!$ligne) {
            Auth::sendError('Ligne not found', 404);
        }

        $success = $this->ligneModel->delete($id);

        if ($success) {
            Auth::sendSuccess(null, 'Ligne deleted successfully');
        } else {
            Auth::sendError('Cannot delete ligne with associated buses', 409);
        }
    }
}
?>
