<?php

namespace App\Models;

use App\Traits\HasSortsTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AppSection extends Model
{
    use HasFactory, HasSortsTrait;

    public const DEFAULT_SECTIONS = [
        [
            'key' => 'hero_principal',
            'title' => 'Hero principal',
            'background_color' => '#ffffff',
            'is_active' => true,
            'order_no' => 1,
        ],
        [
            'key' => 'app',
            'title' => 'App',
            'background_color' => '#fff7f2',
            'is_active' => true,
            'order_no' => 2,
        ],
        [
            'key' => 'promociones_beneficios',
            'title' => 'Promociones y beneficios',
            'background_color' => '#ffffff',
            'is_active' => true,
            'order_no' => 3,
        ],
        [
            'key' => 'como_usar',
            'title' => 'Cómo usar la app',
            'background_color' => '#fff7f2',
            'is_active' => true,
            'order_no' => 4,
        ],
        [
            'key' => 'preguntas_frecuentes',
            'title' => 'Preguntas frecuentes',
            'background_color' => '#ffffff',
            'is_active' => true,
            'order_no' => 5,
        ],
    ];

    protected $table = 'app_sections';

    protected $fillable = [
        'key',
        'title',
        'background_color',
        'is_active',
        'order_no',
    ];

    protected $casts = [
        'is_active' => 'bool',
        'order_no' => 'integer',
        'created_at' => 'datetime:d-m-Y H:i:s',
        'updated_at' => 'datetime:d-m-Y H:i:s',
    ];

    protected $allowedSorts = [
        'id',
        'key',
        'title',
        'background_color',
        'is_active',
        'order_no',
        'created_at',
    ];

    public static function ensureDefaults(): void
    {
        foreach (self::DEFAULT_SECTIONS as $section) {
            self::query()->firstOrCreate(
                ['key' => $section['key']],
                $section
            );
        }
    }
}
