<?php

namespace App\Repositories\Shop;

use App\Models\Shop\Tariff;

class Tariffs
{
    public static function autocomplete($str)
    {
        $data = Tariff::byAutocomplete($str)->orderBy('name')->limit(30)->get()->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 getOptions()
    {
        return Tariff::orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray();
    }

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

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

    public static function getAll()
    {
        return Tariff::orderBy('name', 'asc')->get();
    }

    public static function get($id)
    {
        return Tariff::find($id);
    }

    public static function store($data)
    {
        $id = isset($data['id']) ? $data['id'] : false;
        $item = $id ? self::update($data, $id) : self::create($data);
        return $item->id;
    }

    public static function create($data)
    {
        return Tariff::create($data);
    }

    public static function update($data, $id = false)
    {
        $id = $id ? $id : $data['id'];
        $item = self::get($id);
        $item->update($data);
        return $item;
    }

    public static function destroy($id)
    {
        return Tariff::destroy($id);
    }
}
