<?php

namespace App\Models;

use App\Traits\Multitenantable;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Campaign extends Model
{
    use SoftDeletes, Multitenantable;
    
    protected $primaryKey = 'uuid';
    public $incrementing = false;
    protected $keyType = 'string';
    public $imageFields = ['server_banner_url'];

    protected $fillable = [
        'uuid', 
        'tenant_id', 
        'title', 
        'server_banner_url', 
        'start_date', 
        'end_date', 
        'action_type', 
        'action_reference_id', 
        'is_active', 
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    protected $dates = ['deleted_at'];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function outlet_campaigns() {
        return $this->hasMany(OutletCampaign::class, 'campaign_id', 'uuid');
    }

    public function getServerBannerUrlAttribute($value)
    {
        if (!$value) return null;
        
        return asset('storage/' . $value);
    }
}
