<?php

namespace App\Repositories\Shop;

use App\Datatables\Admin\Shop\PriceListsDataTable;
use App\Models\Shop\Tariff;
use App\Traits\Model\Basic;

class Tariffs
{
    use Basic;

    public static function init()
    {
        $model = new PriceListsDataTable();

        return [
            'sale_channels' => SaleChannels::getOptions(),
            'statuses' => Tariffs::getStatuses(),
            'tariff_unities' => TariffUnities::getOptions(),
            'datatables' => [
                'price_lists' => $model->html(),
            ],
        ];
    }

    public static function autocomplete($str)
    {
        $data = Tariff::byAutocomplete($str)->orderBy('name')->limit(30)->pluck('name', 'id');
        $export = [];
        foreach ($data as $key => $name) {
            $export[] = ['value' => $key, 'text' => $name];
        }

        return $export;
    }

    public static function getByOffer($id)
    {
        return Tariff::byOffer($id)->first();
    }

    public static function getPrices($id)
    {
        return Tariff::with(['price_lists.price_list_values', 'price_lists.sale_channel'])->find($id);
    }

    public static function getStatus($status_id)
    {
        return self::getStatuses()[$status_id];
    }

    public static function getStatusBadge($status_id)
    {
        $colors = ['success', 'warning', 'secondary', 'danger'];
        $label = self::getStatus($status_id);
        $color = $colors[$status_id] ?? 'secondary';

        return '<span class="badge badge-'.$color.'">'.$label.'</span>';
    }

    public static function getStatuses()
    {
        return ['Actif', 'Suspendu', 'Invisible', 'Obsolete'];
    }

    public static function getStatusMap()
    {
        return Tariff::pluck('status_id', 'id')->toArray();
    }

    public static function getPriceListCountMap()
    {
        return Tariff::withCount('price_lists')->pluck('price_lists_count', 'id')->toArray();
    }

    public static function getModel()
    {
        return Tariff::query();
    }
}
