<?php

namespace App\Filament\Pages;

use App\Models\PlatformConfig;
use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Schema;

class AppSettings extends Page implements HasForms
{
    use InteractsWithForms;

    protected static string|\BackedEnum|null $navigationIcon  = 'heroicon-o-cog-6-tooth';

    public static function canAccess(): bool
    {
        return auth()->user()?->isAdmin() ?? false;
    }
    protected static ?string $navigationLabel                 = 'App Settings';
    protected static ?string $title                           = 'App Settings';
    protected static ?int $navigationSort                     = 98;

    protected string $view = 'filament.pages.app-settings';

    public ?string $play_store_downloads = '0';
    public ?string $app_store_downloads  = '0';

    public function mount(): void
    {
        $this->play_store_downloads = PlatformConfig::getValue('play_store_downloads', '0');
        $this->app_store_downloads  = PlatformConfig::getValue('app_store_downloads',  '0');
    }

    public function downloadsForm(Schema $schema): Schema
    {
        return $schema
            ->statePath('')
            ->components([
                TextInput::make('play_store_downloads')
                    ->label('Play Store Downloads')
                    ->helperText('Manually enter the total download count from Google Play')
                    ->numeric()
                    ->prefix('Android')
                    ->required(),

                TextInput::make('app_store_downloads')
                    ->label('App Store Downloads')
                    ->helperText('Manually enter the total download count from Apple App Store')
                    ->numeric()
                    ->prefix('iOS')
                    ->required(),
            ]);
    }

    public function saveDownloads(): void
    {
        PlatformConfig::setValue('play_store_downloads', $this->play_store_downloads);
        PlatformConfig::setValue('app_store_downloads',  $this->app_store_downloads);

        Notification::make()
            ->title('Download counts updated')
            ->success()
            ->send();
    }

    protected function getForms(): array
    {
        return ['downloadsForm'];
    }
}
