<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Promocion extends Model
{
    protected $table = 'promociones';

    protected $guarded = [];

    protected $casts = [
        'fecha_inicio' => 'date',
        'fecha_fin'    => 'date',
    ];

    public function planteles(): BelongsToMany
    {
        return $this->belongsToMany(Plantel::class, 'plantel_promocion');
    }

    public function alumnos(): BelongsToMany
    {
        return $this->belongsToMany(Alumno::class, 'alumno_promocion')
            ->withPivot(['enviado_at', 'status_envio', 'whatsapp_mensaje_id']);
    }

    public function isActiva(): bool
    {
        return $this->status === 'activo';
    }

    public function imagenUrl(): ?string
    {
        if (!$this->imagen) return null;

        if (str_starts_with($this->imagen, 'http')) {
            return $this->imagen;
        }

        return asset('storage/' . $this->imagen);
    }
}
