<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ServiceTypeResource\Pages;
use App\Filament\Resources\ServiceTypeResource\Widgets\ServiceTypeOverview;
use App\Models\ServiceType;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class ServiceTypeResource extends Resource
{
    protected static ?string $model = ServiceType::class;

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

    protected static ?string $navigationGroup = 'Reports';

    protected static ?string $label = 'Product Services';

    public static function getWidgets(): array
    {
        return [
            ServiceTypeOverview::class,
        ];
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->label('Code'),

                Tables\Columns\TextColumn::make('value')
                    ->label('Long Name')
                    ->extraHeaderAttributes(['class' => 'w-full']),

                Tables\Columns\TextColumn::make('quantity')
                    ->alignRight()
                    ->getStateUsing(function (Table $table, Model $record) {
                        return $record->getQuantityAttribute(
                            $table->getFilter('created_at')->getState()['created_at'],
                            $table->getFilter('created_until')->getState()['created_until']
                        );
                    }),

                Tables\Columns\TextColumn::make('amount')
                    ->alignRight()
                    ->getStateUsing(function (Table $table, Model $record) {
                        return $record->getAmountAttribute(
                            $table->getFilter('created_at')->getState()['created_at'],
                            $table->getFilter('created_until')->getState()['created_until']
                        );
                    })
                    ->money('usd'),

                Tables\Columns\TextColumn::make('salesPercentage')
                    ->alignRight()
                    ->getStateUsing(function (Table $table, Model $record) {
                        return $record->getSalesPercentageAttribute(
                            $table->getFilter('created_at')->getState()['created_at'],
                            $table->getFilter('created_until')->getState()['created_until']
                        );
                    })
                    ->suffix('%')
                    ->label('% sales'),

                Tables\Columns\TextColumn::make('averagePrice')
                    ->getStateUsing(function (Table $table, Model $record) {
                        return $record->getAveragePriceAttribute(
                            $table->getFilter('created_at')->getState()['created_at'],
                            $table->getFilter('created_until')->getState()['created_until']
                        );
                    })
                    ->alignRight()
                    ->label('Average')
                    ->prefix('$'),
            ])
            ->filters([
                Tables\Filters\Filter::make('created_at')
                    ->form([
                        DatePicker::make('created_at')
                            ->label('From date'),
                    ]),

                Tables\Filters\Filter::make('created_until')
                    ->form([
                        DatePicker::make('created_until')
                            ->label('Until date'),
                    ]),
            ], layout: Tables\Enums\FiltersLayout::AboveContentCollapsible)
            ->actions([
            ])

            ->bulkActions([
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

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