<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Model;

class LandingPage extends Model
{
    protected ?string $table = 'landing_pages';

    protected array $fillable = [
        'title',
        'html_content',
        'description',
        'hashkey',
        'created_by',
        'updated_by',
        'is_active',
    ];

    protected array $casts = [
        'is_active' => 'boolean',
    ];

    /**
     * Get the currently active landing page.
     */
    public static function getActive(): ?self
    {
        return static::where('is_active', true)->first();
    }

    /**
     * Set this landing page as the active one, deactivating all others.
     */
    public function setAsActive(): void
    {
        // Deactivate all other landing pages
        static::where('id', '!=', $this->id)->update(['is_active' => false]);

        // Activate this one
        $this->is_active = true;
        $this->save();
    }

    /**
     * Deactivate this landing page.
     */
    public function deactivate(): void
    {
        $this->is_active = false;
        $this->save();
    }
}
