<?php

namespace App\Repositories\Shop;

use App\Models\Shop\SaleChannel;

class SaleChannels
{
    public static function getDefaultID()
    {
        return self::getDefault()->id;
    }

    public static function getDefault()
    {
        return self::getByCode('clic');
    }

    public static function getByCode($code)
    {
        return SaleChannel::byCode($code)->first();
    }

    public static function getOptions()
    {
        return SaleChannel::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
    }

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

    public static function get($id)
    {
        return SaleChannel::findOrFail($id);
    }

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

    public static function create($data)
    {
        return SaleChannel::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 SaleChannel::destroy($id);
    }
}
