<?php

namespace App\Support;

use App\Models\AdminRole;
use App\Models\AdminUser;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;

/** 接入站点后台主运营角色 slug=site_admin。 */
final class SitePlatformRole
{
    public const SLUG = SiteOperatorRoles::SLUG_SITE_ADMIN;

    public static function resolve(): AdminRole
    {
        return SiteAdminDefaultRolePermissions::ensurePlatformSiteAdminRole();
    }

    public static function id(): int
    {
        return (int) self::resolve()->id;
    }

    public static function idOrFail(): int
    {
        $id = (int) (AdminRole::query()
            ->where('scope_type', AdminRole::SCOPE_SYSTEM)
            ->where('slug', self::SLUG)
            ->where('status', 1)
            ->value('id') ?? 0);

        if ($id <= 0) {
            throw ValidationException::withMessages([
                'role' => ['platform_site_admin_role_missing: run php artisan lottery:admin-auth-sync'],
            ]);
        }

        return $id;
    }

    public static function userHasSiteAdminRole(AdminUser $user): bool
    {
        return SiteOperatorRoles::userHasSiteAdminRole($user);
    }
}
