<?php

namespace App\Repositories\Shop;

use App\Models\Shop\Invoice;
use App\Traits\Model\Basic;
use Carbon\Carbon;
use Illuminate\Support\Str;

class Invoices
{
    use Basic;

    public static function getInvoiceHtml($id)
    {
        $invoice = self::getFull($id);
        $order = $invoice->order;
        $customer = $order->customer;
        unset($invoice->order, $order->customer);
        $data = [
            'invoice' => $invoice->toArray(),
            'order' => $order->toArray(),
            'customer' => $customer->toArray(),
        ];

        return view('Shop.Invoices.mail', $data)->render();
    }

    public static function init()
    {
        return [
            'statuses' => self::statuses(),
            'payment_types' => InvoicePayments::paymentTypes(),
        ];
    }

    public static function view($uuid)
    {
        $invoice = self::getFullByUUID($uuid);

        if (! $invoice) {
            return false;
        }

        $data = $invoice->toArray();
        $data['payment_type'] = InvoicePayments::getPaymentType($data['payment_type']);
        $data['status'] = self::getStatus($data['status']);

        return $data;
    }

    public static function checkPayments($invoice_id)
    {
        $invoice = self::get($invoice_id);
        $total = self::getPayments($invoice_id);
        if ($total) {
            $status = $total === (float) $invoice->total_shipped ? 2 : 1;
        } else {
            $status = 0;
        }
        $invoice->status = $status;
        $invoice->save();
    }

    public static function getPayments($invoice_id)
    {
        $total = 0;
        $invoice = self::get($invoice_id);
        $payments = $invoice->payments;
        foreach ($payments as $payment) {
            $total += $payment->amount;
        }

        return $total;
    }

    public static function getFullByUUID($id)
    {
        return self::getByUUID($id, self::full());
    }

    public static function getFull($id)
    {
        return self::get($id, self::full());
    }

    public static function full()
    {
        return [
            'address',
            'customer',
            'order.delivery_address',
            'order.detail',
            'order.sale_channel',
            'order.customer',
            'payments',
        ];
    }

    public static function saveInvoice($orderId, $data)
    {
        $data['order_id'] = $orderId;
        $data['date_invoice'] = date('Y-m-d');
        $data['date_due'] = Carbon::now()->addMonth()->format('Y-m-d');

        return self::store($data);
    }

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

        return Invoice::create($data);
    }

    public static function delete($id)
    {
        $invoice = self::get($id);
        InvoiceStats::decrease($invoice->total);

        return Invoice::destroy($id);
    }

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

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

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

    public static function statuses()
    {
        return ['En attente', 'Paiement partiel', 'Soldée'];
    }

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