<?php

namespace App\Filament\Resources\ClientResource\RelationManagers;

use App\Filament\Resources\SaleResource;
use App\Models\Sale;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class SalesRelationManager extends RelationManager
{
    protected static string $relationship = 'sales';

    protected static ?string $title = 'Sales';

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

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('title')
            ->columns([
                TextColumn::make('id')->label('ID')->sortable(),
                TextColumn::make('branch.code')->label('Branch')->sortable(),
                TextColumn::make('happened_on')->label('Date')->date()->sortable(),
                TextColumn::make('gross_amount')->label('Gross Amount')->numeric()->sortable(),
                TextColumn::make('exempt')->label('Exempt')->numeric()->sortable(),
                TextColumn::make('vatable_amount')->label('Vatable Amount')->numeric()->sortable(),
                TextColumn::make('output_tax')->label('Output Tax')->numeric()->sortable(),
                TextColumn::make('payable_withholding_tax')->label('Payable Withholding Tax')->numeric()->sortable(),
                TextColumn::make('discount')->label('Discount')->numeric()->sortable(),
                TextColumn::make('net_amount')->label('Net Amount')->numeric()->sortable(),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\CreateAction::make()
                    ->url(fn () => SaleResource::getUrl('create', ['client_id' => $this->getOwnerRecord()->id])),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->url(fn (Sale $record) => SaleResource::getUrl('edit', ['record' => $record])),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }
}
