<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\OffersTypes;
use Illuminate\Notifications\Notifiable;
use App\Models\Domain;
use App\Models\Filiere;
 
class Offers extends Model 
{
    use HasFactory, Notifiable;

    protected $table = 'offers'; 

    protected $fillable = [
        'reference',
        'title',
        'description',
        'status',
        'keywords',
        'url',
        'company_name',
        'start_date',
        'end_date',
    ];
 
    public function offersTypes()
    {
        return $this->belongsToMany(OfferType::class, 'offer_offer_type', 'offer_id', 'offer_type_id');
    }

    public function domains()
    {
        return $this->belongsToMany(Domains::class, 'offer_domain', 'offer_id', 'domain_id');
    }

    public function filieres()
    {
        return $this->belongsToMany(Filiere::class, 'offer_filiere', 'offer_id', 'filiereid');
    }
    public function getStatusLabelAttribute()
    {
        $statuses = [
            'pending' => 'En attente',
            'published' => 'Publié',
            'stopped' => 'Arrêté',
        ];

        return $statuses[$this->status] ?? 'Inconnu';
    }

    public function isActive()
    {
        $today = now()->toDateString();
        return $this->start_date <= $today && $this->end_date >= $today;
    }
  
}
