<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class Bagage_model extends CI_Model {
    
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    /**
     * Ajouter bagages à une réservation
     */
    public function ajouter_bagages($code_reservation, $bagages) {
        foreach ($bagages as $bagage) {
            $bagage_data = array(
                'code_reservation' => $code_reservation,
                'type_bagage' => isset($bagage['type']) ? $bagage['type'] : 'soute',
                'description' => $bagage['description'],
                'poids_kg' => floatval($bagage['poids']),
                'dimensions' => isset($bagage['dimensions']) ? $bagage['dimensions'] : '',
                'valeur_declaree' => floatval(isset($bagage['valeur']) ? $bagage['valeur'] : 0),
                'etiquette_numero' => $this->generer_etiquette_bagage($code_reservation)
            );

            $this->db->insert('bagages', $bagage_data);
        }
    }

    /**
     * Générer numéro étiquette bagage
     */
    private function generer_etiquette_bagage($code_reservation) {
        return 'BAG' . $code_reservation . '_' . time();
    }

    /**
     * Obtenir bagages par réservation
     */
    public function get_by_reservation($code_reservation) {
        $this->db->where('code_reservation', $code_reservation);
        return $this->db->get('bagages')->result();
    }


    /**
     * Obtenir les bagages d'un voyage
     */
    public function get_by_voyage($voyage_id) {
        $this->db->select('bagages.*, reservations.nom, reservations.prenom, 
                        reservations.code_reservation');
        $this->db->from('bagages');
        $this->db->join('reservations', 'bagages.code_reservation = reservations.code_reservation');
        $this->db->where('reservations.voyage_id', $voyage_id);
        $this->db->where('reservations.statut_reservation', 'ACTIVE');
        $this->db->order_by('bagages.created_at', 'DESC');
        
        return $this->db->get()->result();
    }


    /**
     * Obtenir les statistiques des bagages pour les passagers EMBARQUÉS d'un voyage
     * @param int $voyage_id
     * @return object Nombre de bagages et poids total
     */
    public function get_stats_embarques($voyage_id) {
        $this->db->select('COUNT(bagages.id) as nombre, COALESCE(SUM(bagages.poids_kg), 0) as poids_total');
        $this->db->from('bagages');
        $this->db->join('reservations', 'bagages.code_reservation = reservations.code_reservation');
        $this->db->join('billets', 'reservations.code_reservation = billets.code_reservation AND billets.voyage_id = reservations.voyage_id');
        $this->db->where('reservations.voyage_id', $voyage_id);
        $this->db->where('billets.statut_billet', 'EMBARQUE');
        
        $result = $this->db->get()->row();
        
        return (object) array(
            'total_bagages' => $result ? intval($result->nombre) : 0,
            'poids_total_kg' => $result ? floatval($result->poids_total) : 0
        );
    }

    /**
     * Obtenir les bagages des passagers EMBARQUÉS d'un voyage
     * @param int $voyage_id
     * @return array Liste des bagages avec infos passager
     */
    public function get_bagages_embarques($voyage_id) {
        $this->db->select('bagages.*, reservations.nom, reservations.prenom, reservations.code_reservation');
        $this->db->from('bagages');
        $this->db->join('reservations', 'bagages.code_reservation = reservations.code_reservation');
        $this->db->join('billets', 'reservations.code_reservation = billets.code_reservation AND billets.voyage_id = reservations.voyage_id');
        $this->db->where('reservations.voyage_id', $voyage_id);
        $this->db->where('billets.statut_billet', 'EMBARQUE');
        $this->db->order_by('reservations.nom', 'ASC');
        
        return $this->db->get()->result();
    }



}

?>