<?php

namespace App\Support;

use App\Models\AdminRole;

/** 平台「站点财务」内置角色：对账、报表导出、结算收付，不含代理树经营。 */
final class SiteFinanceDefaultRolePermissions
{
    /** @var list<string> */
    private const TEMPLATE_SLUGS = [
        'prd.dashboard.view',
        'prd.draw_result.view',
        'prd.users.view_finance',
        'prd.tickets.view',
        'prd.report.view',
        'prd.report.export',
        'prd.wallet_reconcile.manage',
        'prd.wallet_adjust.manage',
        'prd.settlement.agent.view',
        'prd.settlement.agent.manage',
    ];

    /** @return list<string> */
    public static function templateSlugs(): array
    {
        return self::TEMPLATE_SLUGS;
    }

    public static function ensurePlatformSiteFinanceRole(): AdminRole
    {
        $role = AdminRole::query()->updateOrCreate(
            [
                'slug' => SiteOperatorRoles::SLUG_SITE_FINANCE,
                'scope_type' => AdminRole::SCOPE_SYSTEM,
            ],
            [
                'code' => SiteOperatorRoles::SLUG_SITE_FINANCE,
                'name' => '站点财务',
                'description' => '单站财务/对账：钱包流水、对账、经营报表导出、信用结算收付',
                'status' => 1,
                'is_system' => true,
                'sort_order' => 41,
                'owner_agent_id' => null,
                'delegated_from_role_id' => null,
            ],
        );

        $role->syncLegacyPermissionSlugs(
            AdminPermissionInheritance::expand(self::TEMPLATE_SLUGS),
        );

        return $role->fresh() ?? $role;
    }
}
