<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Filament\Pages;

use App\Filament\Concerns\InteractsWithDberpPermissions;
use App\Filament\Concerns\RegistersDberpNavigationWhenAccessible;
use App\Filament\Pages\Concerns\InteractsWithDberpNavigationGroupBreadcrumbs;
use App\Filament\Support\DberpFilamentInfolistIconResolver;
use App\Services\DberpPermissionService;
use App\Support\DberpSystemInformationDisplay;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Infolists\Components\TextEntry;
use Filament\Pages\Page;
use Filament\Schemas\Components\Actions;
use Filament\Schemas\Components\EmbeddedSchema;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Enums\FontWeight;
use Illuminate\Contracts\Support\Htmlable;
use UnitEnum;

/**
 * @property-read Schema $content
 * @property-read Schema $infolist
 */
final class DberpSystemInformationPage extends Page
{
    use InteractsWithDberpNavigationGroupBreadcrumbs;
    use InteractsWithDberpPermissions;
    use RegistersDberpNavigationWhenAccessible;

    protected static ?string $slug = 'system-information';

    protected static ?int $navigationSort = 100;

    public static function getNavigationGroup(): string|UnitEnum|null
    {
        return __('dberp.nav.system');
    }

    public static function getNavigationLabel(): string
    {
        return __('dberp.labels.system_information');
    }

    public static function getNavigationIcon(): string|BackedEnum|Htmlable|null
    {
        return null;
    }

    public static function canAccess(): bool
    {
        return self::allowsPermission(DberpPermissionService::VIEW_SYSTEM_SETTINGS);
    }

    public function getTitle(): string|Htmlable
    {
        return __('dberp.system_information.page_title');
    }

    public function getSubheading(): string|Htmlable|null
    {
        return __('dberp.system_information.intro');
    }

    /**
     * @return array<Action>
     */
    protected function getHeaderActions(): array
    {
        $display = app(DberpSystemInformationDisplay::class);

        return [
            Action::make('openOfficialServiceCenter')
                ->label(__('dberp.system_information.actions.open_official_service_center'))
                ->icon('heroicon-o-arrow-top-right-on-square')
                ->color('info')
                ->url(fn (): ?string => $display->officialServiceCenterUrl())
                ->openUrlInNewTab()
                ->visible(fn (): bool => filled($display->officialServiceCenterUrl())),
        ];
    }

    public function content(Schema $schema): Schema
    {
        return $schema->components([
            EmbeddedSchema::make('infolist'),
        ]);
    }

    public function defaultInfolist(Schema $schema): Schema
    {
        if (! $schema->hasCustomColumns()) {
            $schema->columns(1);
        }

        return $schema
            ->inlineLabel(false)
            ->record(null);
    }

    public function infolist(Schema $schema): Schema
    {
        $display = app(DberpSystemInformationDisplay::class);
        $authOverview = $display->authorizationOverviewSummary();

        return $schema->components([
            Section::make(__('dberp.system_information.sections.product'))
                ->icon(DberpFilamentInfolistIconResolver::section('product'))
                ->iconColor('primary')
                ->schema([
                    TextEntry::make('product_name')
                        ->label(__('dberp.system_information.rows.product_name'))
                        ->state($display->productName())
                        ->weight(FontWeight::SemiBold)
                        ->columnSpanFull(),
                    TextEntry::make('current_version')
                        ->label(__('dberp.system_information.rows.current_version'))
                        ->state($display->currentVersion())
                        ->badge()
                        ->color('primary')
                        ->copyable(),
                    TextEntry::make('product_status')
                        ->label(__('dberp.system_information.rows.product_status'))
                        ->state($display->productStatus())
                        ->badge()
                        ->color(fn (): string => $display->productStatusBadgeColor()),
                    TextEntry::make('release_date')
                        ->label(__('dberp.system_information.rows.release_date'))
                        ->state($display->releaseDate())
                        ->visible(fn (): bool => filled($display->releaseDate())),
                ])
                ->columns(2),
            Section::make(__('dberp.system_information.sections.license'))
                ->icon(DberpFilamentInfolistIconResolver::section('license'))
                ->iconColor('gray')
                ->schema([
                    TextEntry::make('usage_version')
                        ->label(__('dberp.system_information.rows.usage_version'))
                        ->state($display->usageVersionLabel())
                        ->badge()
                        ->color(fn (): string => $display->usageVersionBadgeColor()),
                    TextEntry::make('commercial_authorization')
                        ->label(__('dberp.system_information.rows.commercial_authorization'))
                        ->state($display->commercialAuthorizationLabel())
                        ->badge()
                        ->color(fn (): string => $display->commercialAuthorizationBadgeColor()),
                    TextEntry::make('license_holder')
                        ->label(__('dberp.system_information.rows.license_holder'))
                        ->state($display->licenseHolder())
                        ->visible(fn (): bool => $display->hasCommercialLicense() && filled($display->licenseHolder())),
                    TextEntry::make('license_valid_until')
                        ->label(__('dberp.system_information.rows.license_valid_until'))
                        ->state($display->licenseValidUntil())
                        ->visible(fn (): bool => $display->hasCommercialLicense() && filled($display->licenseValidUntil())),
                    TextEntry::make('copyright_notice')
                        ->label(__('dberp.system_information.rows.copyright_notice'))
                        ->state($display->copyrightNotice())
                        ->columnSpanFull(),
                    TextEntry::make('license_explanation')
                        ->label(__('dberp.system_information.rows.license_explanation'))
                        ->state($display->licenseExplanation())
                        ->visible(fn (): bool => filled($display->licenseExplanation()))
                        ->columnSpanFull(),
                ])
                ->columns(2)
                ->footer(fn (): array => $display->shouldShowCopyrightLicensePurchaseLink()
                    ? [
                        Actions::make([
                            Action::make('purchaseCopyrightLicense')
                                ->label(__('dberp.system_information.actions.purchase_copyright_license'))
                                ->icon('heroicon-o-arrow-top-right-on-square')
                                ->color('primary')
                                ->url(fn (): ?string => $display->officialServiceCenterUrl())
                                ->openUrlInNewTab(),
                        ]),
                    ]
                    : []),
            Section::make(__('dberp.system_information.sections.authorization_overview'))
                ->icon(DberpFilamentInfolistIconResolver::section('authorization_overview'))
                ->iconColor('primary')
                ->schema([
                    TextEntry::make('overview_authorization_status')
                        ->label(__('dberp.system_information.authorization_overview.rows.authorization_status'))
                        ->state($authOverview['authorization_status_label'])
                        ->badge()
                        ->color(fn (): string => $authOverview['authorization_status_color']),
                    TextEntry::make('overview_site_binding')
                        ->label(__('dberp.system_information.authorization_overview.rows.site_binding'))
                        ->state($display->siteOfficialBindingLabel((bool) $authOverview['is_site_bound']))
                        ->badge()
                        ->color(fn (): string => $display->siteOfficialBindingBadgeColor((bool) $authOverview['is_site_bound'])),
                    TextEntry::make('overview_official_service_status')
                        ->label(__('dberp.system_information.authorization_overview.rows.official_service_status'))
                        ->state($authOverview['service_status_label'])
                        ->badge()
                        ->color(fn (): string => $authOverview['service_status_color']),
                    TextEntry::make('overview_service_expires_at')
                        ->label(__('dberp.system_information.authorization_overview.rows.service_expires_at'))
                        ->state($authOverview['expires_at_label'])
                        ->visible(fn (): bool => $display->hasOfficialServiceExpiry($authOverview['expires_at_label'])),
                    TextEntry::make('overview_remaining_service_days')
                        ->label(__('dberp.system_information.authorization_overview.rows.remaining_service_days'))
                        ->state($authOverview['remaining_days_label'])
                        ->visible(fn (): bool => $display->hasOfficialRemainingServiceDays($authOverview['remaining_days_label'])),
                ])
                ->columns(2),
        ]);
    }
}
