Files
app-agent/app/Filament/Pages/Tenancy/EditMobileAppProfile.php
T
2026-08-24 19:44:24 +03:30

92 lines
5.2 KiB
PHP

<?php
namespace App\Filament\Pages\Tenancy;
use App\Jobs\SyncCafeBazaarApp;
use App\Models\MobileApp;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Pages\Tenancy\EditTenantProfile;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
class EditMobileAppProfile extends EditTenantProfile
{
public static function getLabel(): string
{
return 'App settings';
}
public function form(Schema $schema): Schema
{
return $schema->components([
Section::make('App identity and availability')->schema([
TextInput::make('name')->required()->maxLength(255),
TextInput::make('package_name')->required()->regex('/^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+$/')->unique(MobileApp::class, ignoreRecord: true)->maxLength(255),
Toggle::make('is_active')->required(),
Toggle::make('maintenance_mode')->required(),
TextInput::make('latest_version_name')->maxLength(255),
TextInput::make('latest_version_code')->numeric()->minValue(1),
TextInput::make('minimum_version_code')->numeric()->minValue(1),
TextInput::make('store_url')->url(),
TextInput::make('privacy_url')->url(),
TextInput::make('support_email')->email(),
])->columns(2)->columnSpanFull(),
Section::make('API access')->description('Use the client key in Android requests. Keep the management key only on trusted servers and in this panel.')->schema([
TextInput::make('client_key')->password()->revealable()->required(),
TextInput::make('management_key')->password()->revealable()->required(),
])->columns(2)->columnSpanFull(),
Section::make('Remote configuration')->schema([
KeyValue::make('configuration')->keyLabel('Key')->valueLabel('Value')->columnSpanFull(),
KeyValue::make('feature_flags')->keyLabel('Feature')->valueLabel('Enabled or value')->columnSpanFull(),
])->columnSpanFull(),
Section::make('Cafe Bazaar server API')->description('Per-app tokens from Pishkhan. Secrets are encrypted in the database.')->schema([
TextInput::make('bazaar_api_secret')->password()->revealable()->label('Billing and Pishkhan API secret'),
TextInput::make('bazaar_comments_header')->label('Comments API header name')->default('CAFEBAZAAR-PISHKHAN-API-SECRET'),
TextInput::make('bazaar_comments_secret')->password()->revealable()->label('Comments API secret'),
Textarea::make('bazaar_billing_public_key')->rows(5)->label('RSA billing public key')->columnSpanFull(),
])->columns(2)->columnSpanFull(),
Section::make('Cafe Bazaar synchronization')->description('Refreshes known purchases and subscriptions, imports reviews, and reconciles release state in the background.')->schema([
Toggle::make('bazaar_sync_enabled')->label('Automatic synchronization')->required(),
TextInput::make('bazaar_sync_interval_minutes')->label('Subscription sync interval (minutes)')->numeric()->minValue(5)->maxValue(1440)->required(),
Placeholder::make('bazaar_sync_status')->label('Last result')->content(fn (?MobileApp $record): string => ucfirst($record?->bazaar_sync_status ?? 'idle')),
Placeholder::make('bazaar_last_synced_at')->label('Last completed')->content(fn (?MobileApp $record): string => $record?->bazaar_last_synced_at?->diffForHumans() ?? 'Never'),
Placeholder::make('bazaar_sync_error')->label('Last error')->content(fn (?MobileApp $record): string => $record?->bazaar_sync_error ?? 'None')->columnSpanFull(),
])->columns(2)->columnSpanFull(),
Section::make('Login with Bazaar')->schema([
TextInput::make('bazaar_login_client_id')->label('Client ID'),
TextInput::make('bazaar_login_client_secret')->password()->revealable()->label('Client secret'),
TextInput::make('bazaar_login_redirect_url')->url()->label('Registered redirect URL')->columnSpanFull(),
])->columns(2)->columnSpanFull(),
]);
}
protected function getHeaderActions(): array
{
return [
Action::make('syncBazaar')
->label('Sync Bazaar now')
->icon(Heroicon::ArrowPath)
->requiresConfirmation()
->action(function (): void {
/** @var MobileApp $app */
$app = Filament::getTenant();
SyncCafeBazaarApp::dispatch($app->id, true);
Notification::make()
->title('Cafe Bazaar synchronization queued')
->body('The dashboard status will update when the background worker finishes.')
->success()
->send();
}),
];
}
}