<?php

namespace App\Filament\Admin\Resources;

use App\Models\Customer;
use App\Models\Invoice;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\Summarizers\Summarizer;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;

class CustomerReportResource extends Resource
{
    protected static ?string $model = Customer::class;

    protected static ?string $navigationGroup = 'Reports';

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

    protected static ?string $navigationLabel = 'Customer Sales';

    protected static ?int $navigationSort = 2;

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

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('company_name')
                    ->label('Customer')
                    ->sortable()
                    ->searchable()
                    ->extraHeaderAttributes(['class' => 'w-full']),

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

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

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

                Tables\Columns\TextColumn::make('total')
                    ->money()
                    ->summarize(Summarizer::make()->using(function (Builder $query, Table $table) {
                        return '$'.
                            number_format(
                                round(Invoice::whereBetween('date', [
                                    $table->getFilter('created_at')->getState()['created_at'] ?? '1900-01-01',
                                    $table->getFilter('created_until')->getState()['created_until'] ?? '2100-01-01',
                                ])->sum('total'), 2), 2, '.', ',');

                    }))
                    ->weight('bold')
                    ->alignRight()
                    ->getStateUsing(function (Table $table, Model $record) {
                        return $record->getTotalAttribute(
                            $table->getFilter('created_at')->getState()['created_at'],
                            $table->getFilter('created_until')->getState()['created_until']
                        );
                    }),
            ])
            ->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'),
                    ]),
            ])
            ->actions([
            ])
            ->bulkActions([
            ]);
    }

    public static function canAccess(): bool
    {
        return auth()->user()->is_admin;
    }

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

    public static function getPages(): array
    {
        return [
            'index' => \App\Filament\Admin\Resources\CustomerReportResource\Pages\ListCustomerReports::route('/'),
        ];
    }
}
