<?php

namespace App\Models;

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

class LeadMagnet extends Model
{
    protected $fillable = [
        'title',
        'description',
        'file_path',
        'is_active',
    ];

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

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

        self::query()->firstOrCreate(
            ['title' => 'The 7 Mistakes Forex Traders Repeat Without Knowing'],
            [
                'description' => 'Join the early access waitlist and get beginner-friendly education around mistake tracking, discipline, and performance review.',
                'is_active' => true,
            ]
        );
    }

    public static function active(): ?self
    {
        if (! Schema::hasTable('lead_magnets')) {
            return new self([
                'title' => 'The 7 Mistakes Forex Traders Repeat Without Knowing',
                'description' => 'Join the early access waitlist and get beginner-friendly education around mistake tracking, discipline, and performance review.',
                'is_active' => true,
            ]);
        }

        self::seedDefault();

        return self::query()->where('is_active', true)->latest()->first();
    }
}
