<?php

namespace App\Filament\Resources\CreditProducts;

use App\Filament\Resources\CreditProducts\Pages\CreateCreditProduct;
use App\Filament\Resources\CreditProducts\Pages\EditCreditProduct;
use App\Filament\Resources\CreditProducts\Pages\ListCreditProducts;
use App\Filament\Resources\CreditProducts\Schemas\CreditProductForm;
use App\Filament\Resources\CreditProducts\Tables\CreditProductsTable;
use App\Models\CreditProduct;
use App\Services\StripeCreditProductSyncer;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;

class CreditProductResource extends Resource
{
    protected static ?string $model = CreditProduct::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCreditCard;

    protected static ?string $recordTitleAttribute = 'name';

    public static function getNavigationLabel(): string
    {
        return __('admin.credit_products.navigation.label');
    }

    public static function getModelLabel(): string
    {
        return __('admin.credit_products.navigation.singular');
    }

    public static function getPluralModelLabel(): string
    {
        return __('admin.credit_products.navigation.plural');
    }

    public static function form(Schema $schema): Schema
    {
        return CreditProductForm::configure($schema);
    }

    public static function table(Table $table): Table
    {
        return CreditProductsTable::configure($table);
    }

    public static function syncStripeAction(): Action
    {
        return Action::make('syncStripe')
            ->label(__('admin.credit_products.actions.sync_stripe'))
            ->icon(Heroicon::OutlinedArrowPath)
            ->visible(fn (CreditProduct $record): bool => blank($record->stripe_price_id))
            ->requiresConfirmation()
            ->action(function (CreditProduct $record): void {
                app(StripeCreditProductSyncer::class)->sync($record);

                Notification::make()
                    ->title(__('admin.credit_products.actions.sync_stripe_success'))
                    ->success()
                    ->send();
            });
    }

    public static function getPages(): array
    {
        return [
            'index' => ListCreditProducts::route('/'),
            'create' => CreateCreditProduct::route('/create'),
            'edit' => EditCreditProduct::route('/{record}/edit'),
        ];
    }
}
