<?php

namespace App\Livewire\Datatable\Studio\Finance;

use App\Enums\SalaryAdjustmentType;
use App\Models\Payroll;
use App\Traits\Datatable\WithConfiguration;
use App\Traits\Datatable\WithPrependColumn;
use App\Traits\WithMediaHandler;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Blade;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn;

class PayrollsTable extends DataTableComponent
{
    use WithConfiguration, WithMediaHandler, WithPrependColumn;

    protected $model = Payroll::class;

    public function columns(): array
    {
        return [
            Column::make('Pegawai', 'user.employee.full_name')->searchable(),

            Column::make('Bulan', 'period_month')
                ->format(fn ($value) => formatDate($value, 'F Y'))
                ->searchable(),

            Column::make('Gaji Pokok', 'base_salary')
                ->format(fn ($value) => currency($value, 'Rp'))
                ->searchable(),

            Column::make('Bonus', 'bonus')
                ->format(fn ($value) => currency($value, 'Rp'))
                ->searchable(),

            Column::make('Potongan', 'deduction')
                ->format(fn ($value) => currency($value, 'Rp'))
                ->searchable(),

            Column::make('Total Gaji', 'total_salary')
                ->format(fn ($value) => currency($value, 'Rp'))
                ->searchable(),

            ArrayColumn::make('Rincian')
                ->data(
                    fn ($value, $row) => $row->adjustments
                        ->where('payroll_id', $row->id)
                        ->map(fn ($item) => [
                            'color' => $item->type->value == SalaryAdjustmentType::DEDUCTION->value ? 'text-red-500' : 'text-green-500',
                            'amount' => currency($item->amount, 'Rp'),
                            'description' => $item->description,
                            'id' => $item->id,
                        ])->toArray()
                )
                ->outputFormat(function ($index, $value) {
                    return Blade::render('
                        <div class="flex justify-between items-start text-[13px] leading-tight mb-1">
                            <div>
                                <div class="font-bold {{ $value[\'color\'] }}">{{ $value[\'amount\'] }}</div>
                                <div class="text-gray-400 text-[12px]">{{ $value[\'description\'] }}</div>
                            </div>
                        </div>
                    ', ['value' => $value]);
                })
                ->flexCol(['class' => 'flex-col gap-3']),

            Column::make('Status')
                ->label(fn ($row) => Blade::render('
                    <div class="flex flex-col items-start space-y-1">
                        <flux:badge color="{{ $row->is_paid->color() }}">
                            {{ $row->is_paid->label() }}
                        </flux:badge>
                        <span class="text-xs text-gray-400">
                            {{ $row->paid_at ? formatDateTime($row->paid_at) : "-" }}
                        </span>
                    </div>
                ', ['row' => $row]))
                ->html(),
        ];
    }

    public function builder(): Builder
    {
        return Payroll::select('payrolls.id', 'period_month', 'payrolls.base_salary', 'bonus', 'deduction', 'total_salary', 'is_paid', 'paid_at')
            ->where('period_month', '!=', now()->format('Y-m'))
            ->with(['user', 'user.employee']);
    }
}
