<?php

namespace App\Models\Ecommerce\Product\Order\Shipping;

use App\Models\Ecommerce\Product\Order\Order;
use App\Models\System\Settings\Location\Province;
use App\Models\System\Settings\Location\Region;

class OrderShipping
{
    /*
     * Shipping methods
     *
     * standart - basic
     * express  - fast
     * self_pickup   - self pickup
     * region   - deliver to regions (mr, ah, dz, lb, bn)
     */

    /**
     * Standart shipping
     */
    public const STANDART = 'standart';

    /**
     * Express shipping
     */
    public const EXPRESS = 'express';

    /**
     * On own - self pickup
     */
    public const SELF_PICKUP = 'self_pickup';

    /**
     * Region - shipping to regions (mr, ah, ...)
     */
    public const REGION = 'region';

    /*
     * Shipping method prices
     *
     * standart - 20
     * express - 30
     * self pickup - 0
     * region - 40
     */

    /**
     * Standart shipping price
     */
    public const STANDART_PRICE = 20;

    /**
     * Express shipping price
     */
    public const EXPRESS_PRICE = 30;

    /**
     * Self pickup shipping price
     */
    public const SELF_PICKUP_PRICE = 0;

    /**
     * Region shipping price
     */
    public const REGION_PRICE = 40;

    /**
     * Order delivery times
     *
     * morning
     * evening
     */

    /**
     * Morning time 09 - 12:00
     */
    public const MORNING = '09:00 - 12:00';

    /**
     * Evening time 13:00 - 18:00
     */
    public const EVENING = '13:00 - 18:00';

    /**
     * Default shipping value
     */
    public static function default(): string
    {
        return static::STANDART;
    }

    /**
     * Default shipping time value
     */
    public static function defaultTime(): string
    {
        return static::EVENING;
    }

    /**
     * Shipping values
     */
    public static function values(): array
    {
        return [
            self::STANDART => __('Standart'),
            self::EXPRESS => __('Express'),
            self::SELF_PICKUP => __('Self Pickup'),
            self::REGION => __('Region'),
        ];
    }

    /**
     * Prices
     */
    public static function prices(): array
    {
        return [
            self::STANDART => self::STANDART_PRICE,
            self::EXPRESS => self::EXPRESS_PRICE,
            self::SELF_PICKUP => self::SELF_PICKUP_PRICE,
            self::REGION => self::REGION_PRICE,
        ];
    }

    /**
     * Shipping times
     */
    public static function times(): array
    {
        return [
            self::MORNING => self::MORNING,
            self::EVENING => self::EVENING,
        ];
    }

    /**
     * Get price for specific shipping
     */
    public static function priceFor(string $type): int
    {
        return static::prices()[$type] ?? self::STANDART_PRICE;
    }

    /**
     * Order shipping price
     *
     * @param  Order  $order
     */
    public static function orderShippingPrice($order): int
    {
        if ($order->region === Region::AG) {
            return static::priceFor($order->shipping_method);
        }

        if (in_array($order->province_id, [
            12,
            43,
            45,
            83,
            56,
            22,
        ])) {
            return 30;
        }

        return static::REGION_PRICE;
    }

    /**
     * Formatted shipping method
     */
    public static function formattedShippingMethod(string $shippingMethod): string
    {
        return static::values()[$shippingMethod] ?? __('Standart');
    }

    /**
     * Get full address
     */
    public static function fullAddress($region, $province, $customer_address): string
    {
        $address = '';

        if ($region) {
            $address .= Region::labelFor($region);
        }

        if ($province = Province::where('id', $province)->first('name')) {
            $address .= ', '.$province->name;
        }

        if ($customer_address) {
            $address .= ','.$customer_address;
        }

        return $address;
    }
}
