<?php

namespace App\Repositories\Shop;

use App\Models\Shop\Order;
use App\Repositories\Core\DateStats;
use App\Traits\Model\Basic;
use Illuminate\Support\Str;

class Orders
{
    use Basic, DateStats;

    public static function getByUUID($uuid)
    {
        return Order::byUUID($uuid)->first();
    }

    public static function getFullByUUID($uuid)
    {
        return Order::with(['customer', 'delivery', 'delivery_address', 'detail', 'invoice.address', 'sale_channel'])
            ->byUUID($uuid)->first();
    }

    public static function view($uuid)
    {
        $data = [];
        $order = self::getFullByUUID($uuid);
        $data = $order->toArray();
        $data['payment_type'] = InvoicePayments::getPaymentType($order->payment_type);
        $data['status'] = Orders::getStatus($order->status);
        return $data;
    }

    public static function saveOrder($data)
    {
        $data += $data['basket'];
        $basket = $data['basket'];
        unset($data['basket']);
        $order = self::store($data);
        $detail = OrderDetails::saveBasket($order->id, $basket['detail']);
        $data['ref'] = $order->ref;
        unset($data['comment']);
        unset($data['agree']);
        unset($data['customer_id']);
        unset($data['delivery_id']);
        unset($data['detail']);
        unset($data['payment_type']);
        unset($data['sale_channel_id']);

        return ($order && $detail) ? Invoices::saveInvoice($order->id, $data) : false;
    }

    public static function edit($id)
    {
        return [
            'order' => self::get($id, ['customer', 'invoice.address', 'delivery', 'delivery_address', 'detail'])->toArray(),
            'statuses' => self::statuses(),
            'delivery_types' => DeliveryTypes::getOptions(),
            'payment_types' => self::paymentTypes(),
            'sale_channels' => SaleChannels::getOptions(),
        ];
    }

    public static function create($data)
    {
        OrderStats::increase();
        $data['uuid'] = Str::uuid()->toString();
        $data['ref'] = self::getNewRef();

        return Order::create($data);
    }

    public static function download($id)
    {
        dump($id);
        exit;
    }

    public static function getStatus($id)
    {
        return self::statuses()[$id] ?? false;
    }

    public static function getStatusByName($name)
    {
        $data = array_flip(self::statuses());

        return $data[$name] ?? '';
    }

    public static function statuses()
    {
        return ['En attente', 'Préparation', 'Expédié', 'Livré'];
    }

    public static function getPaymentType($id)
    {
        return self::paymentTypes()[$id] ?? false;
    }

    public static function paymentTypes()
    {
        return ['', 'CARTE BANCAIRE', 'CHEQUE', 'VIREMENT BANCAIRE'];
    }

    public static function getNewRef()
    {
        $ref = date('ymd').'00000';
        $lastRef = Order::orderBy('id', 'desc')->first();

        return $lastRef ? $lastRef->ref + 1 : $ref + 1;
    }

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