<?php

namespace App\DataTables;

use App\Models\ChampionshipTeam;
use Yajra\DataTables\Services\DataTable;

class ChampionshipTeamDataTable extends DataTable
{
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('logo', function (ChampionshipTeam $row) {
                if ($row->logo) {
                    return '<img src="' . asset('storage/' . $row->logo) . '" alt="" class="rounded" style="width:40px;height:40px;object-fit:cover;">';
                }
                return '<span class="badge bg-light text-muted"><i class="ri-image-line"></i></span>';
            })
            ->addColumn('name', function (ChampionshipTeam $row) {
                $link = route('admin.championship-teams.show', $row->id);
                return '<a href="' . $link . '" class="fw-medium text-primary">' . e($row->name) . '</a>';
            })
            ->addColumn('players_count', fn (ChampionshipTeam $row) => (int) ($row->members_count ?? 0))
            ->addColumn('confirmation', fn (ChampionshipTeam $row) =>
                $row->is_confirmed
                    ? '<span class="badge bg-success">Approved</span>'
                    : '<span class="badge bg-warning">Pending</span>'
            )
            ->addColumn('action', fn (ChampionshipTeam $row) =>
                view('admin.championship-teams.partials.actions', ['row' => $row])->render()
            )
            ->rawColumns(['logo', 'name', 'confirmation', 'action'])
            ->addIndexColumn();
    }

    public function query(ChampionshipTeam $model)
    {
        return $model->newQuery()
            ->forIndex()
            ->with(['championship', 'group'])
            ->withCount('members')
            ->applyFilters();
    }

    public function html()
    {
        return $this->builder()
            ->setTableId('championship-teams-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url' => route('admin.championship-teams.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 [
            addCol('logo', __('Team Logo'), false, false, true, true),
            addCol('name', __('dashboard.name'), true, true, true, true),
            addCol('players_count', __('Players'), true, false, true, true),
            addCol('confirmation', __('Status'), false, false, true, true),
            computedCol('action', __('dashboard.actions')),
        ];
    }
}
