<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SiteSetting extends Model
{
    protected $table = 'site_settings';

    protected $fillable = [
        'admin_emails',
        'duplicate_window_days',
        'site_name',
        'site_description',
        'og_image_path',
        'seo_title',
        'seo_description',
        'seo_keywords',
        'og_image_alt',
        'twitter_site',
    ];

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

    public static function current(): self
    {
        return static::query()->firstOrCreate([], [
            'admin_emails' => '',
            'duplicate_window_days' => 30,
            'site_name' => (string) config('app.name'),
            'site_description' => '',
            'og_image_path' => null,
            'seo_title' => null,
            'seo_description' => null,
            'seo_keywords' => null,
            'og_image_alt' => null,
            'twitter_site' => null,
        ]);
    }

    /**
     * @return array<int, string>
     */
    public static function parseEmails(string $raw): array
    {
        return array_values(array_filter(array_map(
            static fn (string $value): string => trim($value),
            preg_split('/[\s,]+/', $raw) ?: []
        ), static fn (string $value): bool => $value !== ''));
    }
}
