<?php

namespace App\DataTables;

use App\Enums\BookingStatusEnum;
use App\Models\Booking;
use Yajra\DataTables\Services\DataTable;

class BookingDataTable extends DataTable
{
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('checkbox', fn (Booking $row) =>
                '<input type="checkbox" class="row-checkbox" value="' . $row->id . '">'
            )
            ->addColumn('booking_reference', function (Booking $row) {
                $link = route('admin.bookings.show', $row->id);
                return '<a href="' . $link . '" class="fw-medium text-primary">' . e($row->booking_reference ?? $row->id) . '</a>';
            })
            ->addColumn('member', fn (Booking $row) => e($row->member?->name ?? '-'))
            ->addColumn('game', fn (Booking $row) => e($row->game?->name ?? '-'))
            ->addColumn('court', fn (Booking $row) => e($row->court?->name ?? '-'))
            ->addColumn('booking_date', fn (Booking $row) => $row->booking_date?->format('Y-m-d') ?? '-')
            ->addColumn('start_time', fn (Booking $row) => $row->start_time ? substr($row->start_time, 0, 5) : '-')
            ->addColumn('status', fn (Booking $row) => BookingStatusEnum::badge($row->status))
            ->addColumn('created_at', fn (Booking $row) => UpdateDate($row->created_at))
            ->addColumn('action', fn (Booking $row) =>
                view('admin.bookings.partials.actions', ['row' => $row])->render()
            )
            ->rawColumns(['checkbox', 'booking_reference', 'status', 'action'])
            ->addIndexColumn();
    }

    public function query(Booking $model)
    {
        return $model->newQuery()->forIndex()->with(['member', 'game', 'court'])->applyFilters();
    }

    public function html()
    {
        $data = $this->getExportData();
        return $this->builder()
            ->setTableId('bookings-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url'  => route('admin.bookings.datatable'),
                'data' => dtFilters($data['exportFilters'])
            ])
            ->orderBy(1, 'desc')
            ->language([
                'url' => asset('/dashboard_assets/js/datatable/datatables/langs/' . app()->getLocale() . '.json'),
            ])
            ->scrollX(true)
            ->dom('Blfrtip')
            ->parameters([
                'select' => ['style' => 'multi', 'selector' => 'td:first-child'],
                'buttons' => dtButtons([]),
            ] + dtLength(10));
    }

    protected function getColumns(): array
    {
        return [
            computedCol('checkbox', '<input type="checkbox" id="check-all">', 'text-center checkbox-col', false, false, 8),
            addCol('booking_reference', __('Reference'), true, true, true, true),
            addCol('member', __('Member'), true, false, true, true),
            addCol('game', __('Game'), true, false, true, true),
            addCol('court', __('Court'), true, false, true, true),
            addCol('booking_date', __('Date'), true, false, true, true),
            addCol('start_time', __('Time'), true, false, true, true),
            addCol('status', __('dashboard.status'), true, false, true, true),
            addCol('created_at', __('dashboard.created_at'), true, false, true, true),
            computedCol('action', __('dashboard.actions')),
        ];
    }

    protected function filename(): string
    {
        return 'Booking_' . date('YmdHis');
    }

    private function getExportData(): array
    {
        return [
            'exportFilters' => [
                'period'           => '.report',
                'status'           => 'input[name="status-filter"]',
                'orderBy'          => '#orderBy-filter',
                'created_at:range' => '#range-date',
            ],
        ];
    }
}
