<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ProductResource\Pages;
use App\Models\Product;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Str;

class ProductResource extends Resource
{
    protected static ?string $model = Product::class;

    protected static ?string $navigationIcon = 'heroicon-o-cube';

    protected static ?string $navigationGroup = 'Store';

    protected static ?int $navigationSort = 1;

    public static function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\TextInput::make('name')
                ->required()->maxLength(190)
                ->live(onBlur: true)
                ->afterStateUpdated(fn (Set $set, ?string $state, ?Product $record) => $record === null ? $set('slug', Str::slug($state ?? '')) : null),
            Forms\Components\TextInput::make('slug')->required()->unique(ignoreRecord: true),
            Forms\Components\Textarea::make('description')->rows(4)->columnSpanFull(),
            Forms\Components\FileUpload::make('image')->disk('public')->directory('products')->image(),
            Forms\Components\TextInput::make('price_cents')
                ->label('Price (cents)')->numeric()->required()->minValue(50)
                ->helperText('e.g. 39900 = $399.00. Stripe product + price sync automatically on save.'),
            Forms\Components\Select::make('currency')
                ->options(['aud' => 'AUD', 'usd' => 'USD', 'nzd' => 'NZD'])
                ->default('aud')->required(),
            Forms\Components\Toggle::make('active')->default(true),
            Forms\Components\TextInput::make('sort')->numeric()->default(0),
        ])->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\ImageColumn::make('image')->disk('public')->label(''),
                Tables\Columns\TextColumn::make('name')->searchable(),
                Tables\Columns\TextColumn::make('price_cents')->label('Price')
                    ->formatStateUsing(fn (int $state, Product $record): string => strtoupper($record->currency).' $'.number_format($state / 100, 2)),
                Tables\Columns\IconColumn::make('active')->boolean(),
                Tables\Columns\IconColumn::make('stripe_price_id')->label('Stripe')
                    ->boolean()->getStateUsing(fn (Product $record) => filled($record->stripe_price_id))
                    ->tooltip(fn (Product $record) => $record->stripe_price_id ?? 'Not synced yet'),
            ])
            ->defaultSort('sort')
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ManageProducts::route('/'),
        ];
    }
}
