<?php

namespace App\Livewire\Datatable\Studio\Information;

use App\Enums\PriceRequestStatus;
use App\Models\PriceRequest;
use App\Traits\Datatable\WithAppendColumn;
use App\Traits\Datatable\WithConfiguration;
use App\Traits\Datatable\WithPrependColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Blade;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;

class PriceRequestsTable extends DataTableComponent
{
    use WithAppendColumn, WithConfiguration, WithPrependColumn;

    protected $model = PriceRequest::class;

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

            Column::make('module', 'module')
                ->format(fn ($row) => ucfirst($row))
                ->searchable(),

            Column::make('Status', 'status')
                ->label(fn ($row, $column) => Blade::render('<flux:badge color="'.$row->status->color().'">'.$row->status->label().'</flux:badge>'))
                ->html(),

            Column::make('Tanggal')
                ->label(function ($row) {
                    $startDate = formatDate($row->created_at);
                    $startAgo = timeAgo($row->created_at);

                    $endDate = formatDate($row->finalized_at);
                    $endAgo = timeAgo($row->finalized_at);

                    return <<<HTML
                        <div class="flex flex-col text-xs text-gray-700 dark:text-gray-300 gap-0.5">
                            <div>
                                <span class="font-medium text-gray-900 dark:text-white">Tgl Permohonan:</span>
                                <span>{$startDate}</span>
                                <span class="text-gray-500">({$startAgo})</span>
                            </div>
                            <div>
                                <span class="font-medium text-gray-900 dark:text-white">Tgl Ditanggapi:</span>
                                <span>{$endDate}</span>
                                <span class="text-gray-500">({$endAgo})</span>
                            </div>
                        </div>
                    HTML;
                })
                ->html(),

            Column::make('Aksi')
                ->label(function ($row) {
                    $actions = '';

                    if (auth()->user()->can('approve price request') && $row->status === PriceRequestStatus::PENDING) {
                        $actions .= view('components.datatables.confirm', [
                            'id' => $row->hash,
                            'message' => 'Pemohon yang diterima bisa melihat data harga beli.',
                            'confirmButtonText' => 'Ya, Terima',
                            'target' => 'approve',
                            'title' => 'Terima',
                            'confirmButtonColor' => 'primary',
                            'icon' => 'check-circle',
                        ])->render();
                    }

                    if (auth()->user()->can('reject price request') && $row->status === PriceRequestStatus::PENDING) {
                        $actions .= view('components.datatables.confirm', [
                            'id' => $row->hash,
                            'message' => 'Jika ditolak, pomohon tidak dapat melihat data harga beli.',
                            'confirmButtonText' => 'Ya, Tolak',
                            'target' => 'reject',
                            'title' => 'Tolak',
                            'confirmButtonColor' => 'red',
                            'icon' => 'x-circle',
                        ])->render();
                    }

                    return $actions;
                })
                ->html()
                ->hideIf(auth()->user()->cannot('approve price request') && auth()->user()->cannot('reject price request')),
        ];
    }

    public function builder(): Builder
    {
        return PriceRequest::select(
            'price_requests.id',
            'price_requests.user_id',
            'module',
            'price_requests.status',
            'price_requests.created_at',
            'finalized_at'
        )
            ->with(['user', 'user.employee']);
    }
}
