<?php

namespace Database\Seeders;

use Carbon\Carbon;
use Exception;
use File;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class OrderTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $orders = json_decode(File::get('database/data/orders.json'));
        $order_items = json_decode(File::get('database/data/order_items.json'));

        $orders_table = 'orders';
        $order_items_table = 'order_items';

        DB::table($orders_table)->truncate();
        DB::table($order_items_table)->truncate();

        foreach ($orders as $order) {
            try {
                DB::table($orders_table)->insert([
                    'id' => $order->id,
                    'number' => $order->number,
                    'status' => $order->status,
                    'shipping_method' => $order->shipping_method === 'on_own' ? 'self_pickup' : $order->shipping_method,
                    'notes' => $order->notes,
                    'customer_name' => $order->name,
                    'customer_phone' => $order->phone,
                    'customer_address' => $order->address,
                    'delivery_time' => $this->getTime($order->time),
                    'delivery_at' => $this->getDeliveryAt($order->time),
                    'region' => $order->region,
                    'province_id' => $order->province_id,
                    'created_at' => $order->created_at,
                    'updated_at' => $order->updated_at,
                    'payment_type_id' => $this->getPaymentMethod($order->payment_type),
                    'deleted_at' => $order->deleted_at,
                ]);
            } catch (Exception $e) {
                info(['Ignore error', $e->getMessage()]);
            }
        }

        foreach ($order_items as $orderItem) {
            try {
                DB::table($order_items_table)->insert([
                    'id' => $orderItem->id,
                    'product_name' => $orderItem->name,
                    'product_id' => $orderItem->product_id,
                    'order_id' => $orderItem->order_id,
                    'channel_id' => $orderItem->channel_id,
                    'quantity' => $orderItem->quantity,
                    'unit_price_amount' => $orderItem->unit_price_amount,
                    'created_at' => $orderItem->created_at,
                    'updated_at' => $orderItem->updated_at,
                ]);
            } catch (Exception $e) {
                info(['Ignore error', $e->getMessage()]);
            }
        }

        DB::statement("
            SELECT setval('{$orders_table}_id_seq', (SELECT MAX(id) from {$orders_table}))
        ");
        DB::statement("
            SELECT setval('{$order_items_table}_id_seq', (SELECT MAX(id) from {$order_items_table}))
        ");
    }

    public function getTime($data)
    {
        if (! $data) {
            return '';
        }

        $orderDelivery = explode(', ', $data);
        if (count($orderDelivery) < 2) {
            return '';
        }

        return $orderDelivery[0];
    }

    public function getDeliveryAt($data)
    {
        if (! $data) {
            return null;
        }

        $orderDelivery = explode(', ', $data);
        if (count($orderDelivery) < 2) {
            return null;
        }

        return (new Carbon($orderDelivery[1]))->format('Y-m-d');
    }

    /**
     * Get the payment id for method
     */
    public function getPaymentMethod($method): int
    {
        return match ($method) {
            'cash' => 1,
            'atm' => 2,
            'online' => 3,
            default => 1,
        };
    }
}
