<?php

namespace App\Repositories\Shop;

use App\Models\Shop\CustomerAddress;
use App\Traits\Model\Basic;

class CustomerAddresses
{
    use Basic;

    public static function storeByCustomer($customer, $data)
    {
        $deliveries = $data['deliveries'] ?? false;
        if ($deliveries) {
            if (! empty($deliveries['address_id'])) {
                self::setDefault($customer->id, (int) $deliveries['address_id'], 1);
            }

            if (! empty($deliveries['zipcode']) && ! empty($deliveries['city'])) {
                $payload = $deliveries;
                unset($payload['address_id']);
                $payload['customer_id'] = $customer->id;
                $payload['type'] = 1;
                $newAddress = self::store($payload);
                self::setDefault($customer->id, $newAddress->id, 1);
            }
        }

        $invoices = $data['invoices'] ?? false;
        if ($invoices) {
            if (! empty($invoices['address_id'])) {
                self::setDefault($customer->id, (int) $invoices['address_id'], 2);
            }

            if (! empty($invoices['zipcode']) && ! empty($invoices['city'])) {
                $payload = $invoices;
                unset($payload['address_id']);
                $payload['customer_id'] = $customer->id;
                $payload['type'] = 2;
                $newAddress = self::store($payload);
                self::setDefault($customer->id, $newAddress->id, 2);
            }
        }
    }

    public static function createByCustomer($customerId, $data)
    {
        self::addDeliveryAddress($customerId, $data);
        self::addInvoiceAddress($customerId, $data);
    }

    public static function addDeliveryAddress($customerId, $data)
    {
        $delivery = $data['use_for_delivery'] ?? false;
        $data = array_merge($data, [
            'address' => $delivery ? $data['delivery_address'] ?? $data['address'] : $data['address'],
            'address2' => $delivery ? $data['delivery_address2'] ?? $data['address2'] : $data['address2'],
            'zipcode' => $delivery ? $data['delivery_zipcode'] ?? $data['zipcode'] : $data['zipcode'],
            'city' => $delivery ? $data['delivery_city'] ?? $data['city'] : $data['city'],
        ]);

        return self::addAddress($customerId, $data, 2);
    }

    public static function addInvoiceAddress($customerId, $data)
    {
        return self::addAddress($customerId, $data, 1);

    }

    public static function addAddress($customerId, $data, $type)
    {
        $name = $data['company'] ? $data['company'] : $data['first_name'].' '.$data['last_name'];

        $data = [
            'customer_id' => $customerId,
            'type' => $type,
            'name' => $name,
            'address' => $data['address'],
            'address2' => $data['address2'],
            'zipcode' => $data['zipcode'],
            'city' => $data['city'],
        ];

        return self::store($data);
    }

    public static function getInvoiceAddress($customerId)
    {
        $address = CustomerAddress::byCustomer($customerId)
            ->byInvoicing()
            ->orderByDesc('priority')
            ->orderBy('id')
            ->first();

        return $address ?? self::getByCustomer($customerId);
    }

    public static function getDeliveryAddress($customerId)
    {
        $address = CustomerAddress::byCustomer($customerId)
            ->byDelivery()
            ->orderByDesc('priority')
            ->orderBy('id')
            ->first();

        return $address ?? self::getByCustomer($customerId);
    }

    public static function getByCustomer($customerId = false)
    {
        $customer = Customers::get($customerId);

        return $customer ? $customer->only(['address', 'adress2', 'zipcode', 'city']) : false;
    }

    public static function getIconByType($type)
    {
        return ((int) $type === 1) ? '<i class="fa fa-fw fa-truck"></i>' : '<i class="fa fa-fw fa-file-invoice"></i>';
    }

    public static function setDefault($customerId, $addressId, $type)
    {
        if (! $addressId) {
            return;
        }

        $address = self::get($addressId);

        if (! $address || (int) $address->customer_id !== (int) $customerId || (int) $address->type !== (int) $type) {
            return;
        }

        self::getModel()->byCustomer($customerId)->byType($type)->update(['priority' => null]);

        $address->priority = 1;
        $address->save();
    }

    public static function ensureDefault($customerId, $type)
    {
        $hasDefault = self::getModel()->byCustomer($customerId)->byType($type)->where('priority', 1)->exists();

        if ($hasDefault) {
            return;
        }

        $address = self::getModel()->byCustomer($customerId)->byType($type)->orderBy('id')->first();

        if ($address) {
            $address->priority = 1;
            $address->save();
        }
    }

    public static function toggleActive($id, $active)
    {
        return self::update(['active' => $active], $id);
    }

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