<?php
/**
 * Operator Controller
 * Handles operator (bus company) management
 */

class OperatorController {
    private $db;
    private $operatorModel;

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

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

        $filters = [];

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

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

        $operators = $this->operatorModel->getAll($filters);

        Auth::sendSuccess($operators, 'Operators retrieved successfully');
    }

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

        $operator = $this->operatorModel->getById($id);

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

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

        $data = Auth::getRequestBody();

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

        // Check if name already exists
        if ($this->operatorModel->nameExists($data['name'])) {
            Auth::sendError('Operator name already exists', 409);
        }

        $operatorId = $this->operatorModel->create($data);

        if ($operatorId) {
            $operator = $this->operatorModel->getById($operatorId);
            Auth::sendSuccess($operator, 'Operator created successfully', 201);
        } else {
            Auth::sendError('Failed to create operator', 500);
        }
    }

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

        $data = Auth::getRequestBody();

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

        // Check if operator exists
        $existingOperator = $this->operatorModel->getById($id);
        if (!$existingOperator) {
            Auth::sendError('Operator not found', 404);
        }

        // Check name uniqueness if being updated
        if (isset($data['name']) && $data['name'] !== $existingOperator['name']) {
            if ($this->operatorModel->nameExists($data['name'], $id)) {
                Auth::sendError('Operator name already exists', 409);
            }
        }

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

        if ($success) {
            $updatedOperator = $this->operatorModel->getById($id);
            Auth::sendSuccess($updatedOperator, 'Operator updated successfully');
        } else {
            Auth::sendError('Failed to update operator', 500);
        }
    }

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

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

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

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