<?php
/**
 * Home Controller
 * Handle main website pages
 */

namespace Controllers;

use Core\Auth;
use Core\Helpers;

class HomeController extends Controller {
    
    /**
     * Landing page
     */
    public function index($params = []) {
        // If user is logged in, redirect to appropriate dashboard
        if (Auth::check()) {
            $user = Auth::user();
            
            switch ($user['role']) {
                case 'admin':
                    Helpers::redirect('/admin');
                    break;
                case 'pharmacy':
                    Helpers::redirect('/pharmacy');
                    break;
                case 'courier':
                    Helpers::redirect('/courier');
                    break;
                case 'client':
                default:
                    Helpers::redirect('/client');
                    break;
            }
        }
        
        $this->view('home', [
            'title' => 'MediFlash SOS+ - Livraison Médicaments d\'Urgence',
            'csrf_token' => Helpers::generateCsrfToken()
        ]);
    }
    
    /**
     * SOS request page
     */
    public function sos($params = []) {
        $this->view('sos', [
            'title' => 'Demande SOS - ' . APP_NAME,
            'csrf_token' => Helpers::generateCsrfToken(),
            'config' => [
                'SERVICE_FEE' => getConfigValue('SERVICE_FEE', SERVICE_FEE),
                'DELIVERY_MIN' => getConfigValue('DELIVERY_MIN', DELIVERY_MIN),
                'DELIVERY_PER_KM' => getConfigValue('DELIVERY_PER_KM', DELIVERY_PER_KM)
            ]
        ]);
    }
    
    /**
     * Order tracking page
     */
    public function track($params = []) {
        $orderId = $params['id'] ?? null;
        
        if (!$orderId) {
            Helpers::redirect('/');
        }
        
        // Get order details
        $order = $this->db->fetchOne(
            "SELECT o.*, u.name as client_name, p.name as pharmacy_name, 
                    c.user_id as courier_user_id, cu.name as courier_name
             FROM orders o
             LEFT JOIN users u ON o.client_id = u.id
             LEFT JOIN pharmacies p ON o.pharmacy_id = p.id
             LEFT JOIN couriers c ON o.courier_id = c.id
             LEFT JOIN users cu ON c.user_id = cu.id
             WHERE o.id = ?",
            [$orderId]
        );
        
        if (!$order) {
            $this->view('404');
            return;
        }
        
        // Get order events
        $events = $this->db->fetchAll(
            "SELECT * FROM order_events WHERE order_id = ? ORDER BY created_at ASC",
            [$orderId]
        );
        
        $this->view('tracking', [
            'title' => 'Suivi Commande ' . $orderId . ' - ' . APP_NAME,
            'order' => $order,
            'events' => $events
        ]);
    }
    
    /**
     * USSD Simulator page
     */
    public function ussdSim($params = []) {
        $this->view('ussd-sim', [
            'title' => 'Simulateur USSD - ' . APP_NAME,
            'csrf_token' => Helpers::generateCsrfToken()
        ]);
    }
    
    /**
     * Login page
     */
    public function login($params = []) {
        if (Auth::check()) {
            Helpers::redirect('/');
        }
        
        $this->view('auth/login', [
            'title' => 'Connexion - ' . APP_NAME,
            'csrf_token' => Helpers::generateCsrfToken()
        ]);
    }
    
    /**
     * Registration page
     */
    public function register($params = []) {
        if (Auth::check()) {
            Helpers::redirect('/');
        }
        
        $this->view('auth/register', [
            'title' => 'Inscription - ' . APP_NAME,
            'csrf_token' => Helpers::generateCsrfToken()
        ]);
    }
    
    /**
     * About page
     */
    public function about($params = []) {
        $this->view('about', [
            'title' => 'À propos - ' . APP_NAME
        ]);
    }
    
    /**
     * Privacy policy
     */
    public function privacy($params = []) {
        $this->view('privacy', [
            'title' => 'Politique de Confidentialité - ' . APP_NAME
        ]);
    }
    
    /**
     * Terms of service
     */
    public function terms($params = []) {
        $this->view('terms', [
            'title' => 'Conditions d\'Utilisation - ' . APP_NAME
        ]);
    }
    
    /**
     * Contact page
     */
    public function contact($params = []) {
        $this->view('contact', [
            'title' => 'Contact - ' . APP_NAME,
            'csrf_token' => Helpers::generateCsrfToken()
        ]);
    }
    
    /**
     * 404 Not Found page
     */
    public function notFound($params = []) {
        http_response_code(404);
        $this->view('404', [
            'title' => 'Page non trouvée - ' . APP_NAME
        ]);
    }
    
    /**
     * Unauthorized page
     */
    public function unauthorized($params = []) {
        http_response_code(403);
        $this->view('unauthorized', [
            'title' => 'Accès non autorisé - ' . APP_NAME
        ]);
    }
}
?>
