<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PageDefinition extends Model
{
    public const TYPE_SECTION_BASED = 'section_based';
    public const TYPE_HYBRID = 'hybrid';
    public const TYPE_DEDICATED = 'dedicated';

    protected $fillable = [
        'name',
        'slug',
        'page_type',
        'section_ids',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'section_ids' => 'array',
            'is_active' => 'boolean',
        ];
    }

    public static function defaults(): array
    {
        return [
            ['name' => 'Home', 'slug' => 'home', 'page_type' => self::TYPE_SECTION_BASED],
            ['name' => 'Countries', 'slug' => 'countries', 'page_type' => self::TYPE_HYBRID],
            ['name' => 'Country View', 'slug' => 'country-view', 'page_type' => self::TYPE_HYBRID],
            ['name' => 'Events', 'slug' => 'events', 'page_type' => self::TYPE_DEDICATED],
            ['name' => 'Event View', 'slug' => 'event-view', 'page_type' => self::TYPE_DEDICATED],
            ['name' => 'Services', 'slug' => 'services', 'page_type' => self::TYPE_SECTION_BASED],
            ['name' => 'About', 'slug' => 'about', 'page_type' => self::TYPE_SECTION_BASED],
            ['name' => 'Contact', 'slug' => 'contact', 'page_type' => self::TYPE_SECTION_BASED],
            ['name' => 'Success Stories', 'slug' => 'success-stories', 'page_type' => self::TYPE_DEDICATED],
        ];
    }

    public static function ensureDefaults(): void
    {
        foreach (self::defaults() as $page) {
            self::query()->firstOrCreate(
                ['slug' => $page['slug']],
                [
                    'name' => $page['name'],
                    'page_type' => $page['page_type'],
                    'section_ids' => [],
                    'is_active' => true,
                ]
            );
        }
    }
}
