<?php
require_once 'Boleto.php';

class Venta {
    private $id;
    private $fecha;
    private $boletos = [];
    private $total;
    private $nombreCliente;
    
    public function __construct($nombreCliente) {
        $this->id = bin2hex(random_bytes(8));
        date_default_timezone_set('America/Mexico_City');
        $this->fecha = date('Y-m-d H:i:s');
        $this->nombreCliente = $nombreCliente;
        $this->total = 0;
    }
    
    public function getId() {
        return $this->id;
    }
    
    public function getFecha() {
        return $this->fecha;
    }
    
    public function getNombreCliente() {
        return $this->nombreCliente;
    }
    
    public function agregarBoletos($boletos) {
        foreach ($boletos as $boleto) {
            if ($boleto->estaDisponible()) {
                $this->boletos[] = $boleto;
                $this->total += $boleto->getPrecio();
                $boleto->marcarComoVendido();
            }
        }
    }
    
    public function generarComprobante() {
        $comprobante = [
            'id_venta' => $this->id,
            'fecha' => $this->fecha,
            'cliente' => $this->nombreCliente,
            'boletos' => [],
            'total' => $this->total
        ];
        
        foreach ($this->boletos as $boleto) {
            $comprobante['boletos'][] = [
                'id' => $boleto->getId(),
                'fila' => $boleto->getFila(),
                'numero' => $boleto->getNumero(),
                'precio' => $boleto->getPrecio()
            ];
        }
        
        return $comprobante;
    }
    
    public function getTotal() {
        return $this->total;
    }
    
    public function getBoletos() {
        return $this->boletos;
    }
}