<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Slide extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'title',
        'subtitle',
        'background_image',
        'image', // Alias for background_image
        'cta_text',
        'button_text', // Alias for cta_text
        'cta_link',
        'link', // Alias for cta_link
        'order',
        'status',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'title' => 'array',
            'subtitle' => 'array',
            'cta_text' => 'array',
            'button_text' => 'array',
            'order' => 'integer',
        ];
    }

    /**
     * Get the image attribute (alias for background_image).
     */
    public function getImageAttribute()
    {
        return $this->background_image;
    }

    /**
     * Set the image attribute (alias for background_image).
     */
    public function setImageAttribute($value)
    {
        $this->attributes['background_image'] = $value;
    }

    /**
     * Get the button_text attribute (alias for cta_text).
     */
    public function getButtonTextAttribute()
    {
        return $this->cta_text;
    }

    /**
     * Set the button_text attribute (alias for cta_text).
     */
    public function setButtonTextAttribute($value)
    {
        $this->attributes['cta_text'] = $value;
    }

    /**
     * Get the link attribute (alias for cta_link).
     */
    public function getLinkAttribute()
    {
        return $this->cta_link;
    }

    /**
     * Set the link attribute (alias for cta_link).
     */
    public function setLinkAttribute($value)
    {
        $this->attributes['cta_link'] = $value;
    }

    /**
     * Scope a query to only include active slides.
     */
    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

    /**
     * Get the response format matching the frontend structure.
     */
    public function toApiArray(): array
    {
        $backgroundImage = $this->background_image;
        $backgroundImageUrl = $backgroundImage;

        if ($backgroundImage) {
            if (is_string($backgroundImage) && str_starts_with($backgroundImage, 'http')) {
                $backgroundImageUrl = $backgroundImage;
            } elseif (is_string($backgroundImage) && str_starts_with($backgroundImage, '/')) {
                $backgroundImageUrl = url($backgroundImage);
            } else {
                $backgroundImageUrl = asset($backgroundImage);
            }
        }

        return [
            'id' => $this->id,
            'title' => $this->title ?? ['en' => '', 'km' => ''],
            'subtitle' => $this->subtitle ?? ['en' => '', 'km' => ''],
            'backgroundImage' => $backgroundImageUrl,
            'ctaText' => $this->cta_text ?? ['en' => '', 'km' => ''],
            'ctaLink' => $this->cta_link,
            'order' => $this->order,
            'status' => $this->status,
        ];
    }
}
