<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;

class HomepageSection extends Model
{
    protected $fillable = [
        'key',
        'title',
        'subtitle',
        'slogan',
        'cta_text',
        'cta_link',
        'content',
        'is_active',
        'sort_order',
    ];

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

    /**
     * @return array<string,array<string,mixed>>
     */
    public static function defaults(): array
    {
        return [
            'hero' => [
                'key' => 'hero',
                'title' => 'Trade. Journal. Improve.',
                'subtitle' => 'ViZuTrade helps traders see the truth behind every setup: trades, mistakes, emotions, risk, performance, and discipline in one clean trading journal.',
                'slogan' => 'ViZuTrade - Your Trading Mirror',
                'cta_text' => 'Start Journaling',
                'cta_link' => '/register',
                'content' => null,
                'sort_order' => 1,
            ],
            'problem' => [
                'key' => 'problem',
                'title' => 'Traders repeat mistakes because they do not track them.',
                'subtitle' => 'Most traders remember the outcome, but forget the emotion, risk decision, session, plan violation, and mistake that caused the result.',
                'slogan' => 'The Problem',
                'sort_order' => 2,
            ],
            'solution' => [
                'key' => 'solution',
                'title' => 'ViZuTrade shows performance clearly.',
                'subtitle' => 'Your dashboard turns trades into feedback: what you did, what you felt, what you risked, what you repeated, and what to improve next.',
                'slogan' => 'The Solution',
                'sort_order' => 3,
            ],
            'footer' => [
                'key' => 'footer',
                'title' => 'ViZuTrade',
                'subtitle' => 'ViZuTrade - Your Trading Mirror. Trade. Journal. Improve.',
                'sort_order' => 99,
            ],
        ];
    }

    public static function seedDefaults(): void
    {
        if (! Schema::hasTable('homepage_sections')) {
            return;
        }

        foreach (self::defaults() as $default) {
            self::query()->firstOrCreate(['key' => $default['key']], $default + ['is_active' => true]);
        }
    }

    /**
     * @return Collection<string,self>
     */
    public static function activeByKey(): Collection
    {
        if (! Schema::hasTable('homepage_sections')) {
            return collect(self::defaults())
                ->map(fn (array $default): self => new self($default + ['is_active' => true]))
                ->keyBy('key');
        }

        self::seedDefaults();

        return self::query()
            ->where('is_active', true)
            ->orderBy('sort_order')
            ->get()
            ->keyBy('key');
    }
}
