<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

class CustomCard extends Model
{
    protected $fillable = [
        'layout_id',
        'title',
        'description',
        'image',
        'bg_color',
        'text_color',
        'col_span',
        'row_span',
        'link_type',
        'link_url',
        'link_id',
        'show_button',
        'button_text',
        'button_link',
        'is_active',
        'sort_order',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'show_button' => 'boolean',
        'col_span' => 'integer',
        'row_span' => 'integer',
        'link_id' => 'integer',
    ];

    protected $appends = ['image_url'];

    public function getImageUrlAttribute(): ?string
    {
        if (!$this->image) {
            return null;
        }
        // Return full URL including domain
        return url(Storage::url($this->image));
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('sort_order');
    }

    public function layout()
    {
        return $this->belongsTo(CustomCardLayout::class, 'layout_id');
    }
}
