<?php

namespace App\Filament\Resources\Products\ProductResource\RelationManagers;

use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Actions\ViewAction;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Schemas\Schema;
use Filament\Schemas\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Placeholder;

class ContractsRelationManager extends RelationManager
{
    protected static string $relationship = 'contracts';

    protected static ?string $title = 'Lịch sử Giao dịch';

    protected static ?string $modelLabel = 'Hợp đồng';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Thông tin Hợp đồng')
                    ->columns(2)
                    ->schema([
                        TextInput::make('contract_number')->label('Số Hợp đồng'),
                        Select::make('contract_type')
                            ->label('Loại Hợp đồng')
                            ->options([
                                'HĐMB' => 'HĐ Mua bán',
                                'HĐĐC' => 'HĐ Đặt cọc',
                                'HĐGV' => 'HĐ Góp vốn',
                                'VBCN' => 'Văn bản chuyển nhượng',
                            ]),
                        DatePicker::make('signing_date')->label('Ngày ký')->displayFormat('d/m/Y'),
                        TextInput::make('status')->label('Trạng thái'),
                    ]),
                Section::make('Giá trị & Tài chính')
                    ->columns(2)
                    ->schema([
                        Placeholder::make('total_value')
                            ->label('Tổng giá trị')
                            ->content(fn ($record) => $record ? number_format($record->total_value) . ' VNĐ' : '-'),
                        Placeholder::make('paid_amount')
                            ->label('Đã thanh toán')
                            ->content(fn ($record) => $record ? number_format($record->paid_amount) . ' VNĐ' : '-'),
                        Placeholder::make('remaining_amount')
                            ->label('Số tiền còn lại')
                            ->content(fn ($record) => $record ? number_format($record->total_value - $record->paid_amount) . ' VNĐ' : '-'),
                    ]),
                Section::make('Khách hàng liên quan')
                    ->schema([
                        Select::make('customers')
                            ->label('Danh sách Khách hàng')
                            ->relationship('customers', 'full_name')
                            ->multiple()
                            ->preload(),
                    ]),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('contract_number')
            ->columns([
                TextColumn::make('transfer_order')
                    ->label('Thứ tự')
                    ->formatStateUsing(fn ($state) => match ((int) $state) {
                        0 => 'Chủ hiện tại',
                        1 => 'HĐ Gốc',
                        default => 'F' . ($state - 1),
                    })
                    ->color(fn ($state) => match ((int) $state) {
                        0 => 'success',
                        1 => 'gray',
                        default => 'warning',
                    })->badge(),
                TextColumn::make('contract_number')->label('Mã HĐ')->searchable()->sortable(),
                TextColumn::make('contract_type')->label('Loại HĐ')->badge(),
                TextColumn::make('total_value')->label('Giá trị')->money('VND')->sortable(),
                TextColumn::make('signing_date')->label('Ngày ký')->date('d/m/Y')->sortable(),
                TextColumn::make('status')->label('Trạng thái')->badge(),
            ])
            ->defaultSort('transfer_order', 'asc')
            ->actions([
                ViewAction::make(),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
