<?php

namespace App\View\Components\Table\Columns;

use Illuminate\Database\Eloquent\Model;
use App\Exceptions\DataTableConfigurationException;
use App\View\Components\Table\Column;
use App\View\Components\Table\Traits\Configuration\BooleanColumnConfiguration;
use App\View\Components\Table\Traits\Helpers\BooleanColumnHelpers;

class BooleanColumn extends Column
{
    use BooleanColumnConfiguration,
        BooleanColumnHelpers;

    protected string $type = 'icons';
    protected bool $successValue = true;
    protected string $view = 'components.includes.columns.boolean';
    protected $callback;

    public function getContents(Model $row)
    {
        if ($this->isLabel()) {
            throw new DataTableConfigurationException('You can not specify a boolean column as a label.');
        }

        $value = $this->getValue($row);

        return view($this->getView())
            ->withComponent($this->getComponent())
            ->withSuccessValue($this->getSuccessValue())
            ->withType($this->getType())
            ->withStatus($this->hasCallback() ? call_user_func($this->getCallback(), $value, $row) : (bool)$value === true);
    }
}
