<?php

namespace App\DataTables;

use App\Enums\ChampionshipMatchStageEnum;
use App\Enums\ChampionshipMatchStatusEnum;
use App\Models\ChampionshipMatch;
use Yajra\DataTables\Services\DataTable;

class ChampionshipMatchDataTable extends DataTable
{
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('checkbox', fn (ChampionshipMatch $row) =>
                '<input type="checkbox" class="row-checkbox" value="' . $row->id . '">'
            )
            ->addColumn('championship', fn (ChampionshipMatch $row) => e($row->championship?->name ?? '-'))
            ->addColumn('teams', fn (ChampionshipMatch $row) => e(($row->teamA?->name ?? 'TBD') . ' vs ' . ($row->teamB?->name ?? 'TBD')))
            ->addColumn('round', fn (ChampionshipMatch $row) => e($row->round ?: ChampionshipMatchStageEnum::label($row->stage)))
            ->addColumn('date_time', fn (ChampionshipMatch $row) => e(($row->match_date?->format('Y-m-d') ?? '-') . ' ' . ($row->start_time ?? '')))
            ->addColumn('status', fn (ChampionshipMatch $row) => ChampionshipMatchStatusEnum::badge($row->status))
            ->addColumn('action', fn (ChampionshipMatch $row) =>
                view('admin.championship-matches.partials.actions', ['row' => $row])->render()
            )
            ->rawColumns(['checkbox', 'status', 'action'])
            ->addIndexColumn();
    }

    public function query(ChampionshipMatch $model)
    {
        return $model->newQuery()
            ->forIndex()
            ->with(['championship', 'teamA', 'teamB'])
            ->applyFilters()
            ->orderBy('match_date')
            ->orderBy('start_time');
    }

    public function html()
    {
        return $this->builder()
            ->setTableId('championship-matches-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url' => route('admin.championship-matches.datatable'),
                'data' => dtFilters([
                    'period' => '.report',
                    'orderBy' => '#orderBy-filter',
                    'created_at:range' => '#range-date',
                ]),
            ])
            ->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('championship', __('Championship'), true, false, true, true),
            addCol('teams', __('Teams'), false, false, true, true),
            addCol('round', __('Round'), true, false, true, true),
            addCol('date_time', __('Match Date'), false, false, true, true),
            addCol('status', __('dashboard.status'), true, false, true, true),
            computedCol('action', __('dashboard.actions')),
        ];
    }
}
