<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    public function up(): void
    {
        $settings = [
            [
                'title' => 'customer_order_placed',
                'key' => 'customer_order_placed',
                'type' => 'customer',
                'module_type' => 'all',
                'mail_status' => 'disable',
                'sms_status' => 'inactive',
                'push_notification_status' => 'disable',
                'sub_title' => 'Sent_notification_when_customer_order_is_placed',
            ],
            [
                'title' => 'customer_order_confirmed',
                'key' => 'customer_order_confirmed',
                'type' => 'customer',
                'module_type' => 'all',
                'mail_status' => 'disable',
                'sms_status' => 'inactive',
                'push_notification_status' => 'disable',
                'sub_title' => 'Sent_notification_when_customer_order_is_confirmed',
            ],
            [
                'title' => 'customer_order_packaging',
                'key' => 'customer_order_packaging',
                'type' => 'customer',
                'module_type' => 'all',
                'mail_status' => 'disable',
                'sms_status' => 'inactive',
                'push_notification_status' => 'disable',
                'sub_title' => 'Sent_notification_when_customer_order_is_in_packaging',
            ],
            [
                'title' => 'customer_order_handed_to_courier',
                'key' => 'customer_order_handed_to_courier',
                'type' => 'customer',
                'module_type' => 'all',
                'mail_status' => 'disable',
                'sms_status' => 'inactive',
                'push_notification_status' => 'disable',
                'sub_title' => 'Sent_notification_when_customer_order_is_handed_to_courier',
            ],
            [
                'title' => 'customer_order_out_for_delivery',
                'key' => 'customer_order_out_for_delivery',
                'type' => 'customer',
                'module_type' => 'all',
                'mail_status' => 'disable',
                'sms_status' => 'inactive',
                'push_notification_status' => 'disable',
                'sub_title' => 'Sent_notification_when_customer_order_is_out_for_delivery',
            ],
            [
                'title' => 'customer_order_delivered',
                'key' => 'customer_order_delivered',
                'type' => 'customer',
                'module_type' => 'all',
                'mail_status' => 'disable',
                'sms_status' => 'inactive',
                'push_notification_status' => 'disable',
                'sub_title' => 'Sent_notification_when_customer_order_is_delivered',
            ],
            [
                'title' => 'customer_order_returned',
                'key' => 'customer_order_returned',
                'type' => 'customer',
                'module_type' => 'all',
                'mail_status' => 'disable',
                'sms_status' => 'inactive',
                'push_notification_status' => 'disable',
                'sub_title' => 'Sent_notification_when_customer_order_is_returned',
            ],
        ];

        foreach ($settings as $setting) {
            $exists = DB::table('notification_settings')
                ->where('key', $setting['key'])
                ->where('type', $setting['type'])
                ->where('module_type', $setting['module_type'])
                ->exists();

            if (! $exists) {
                DB::table('notification_settings')->insert(array_merge($setting, [
                    'created_at' => now(),
                    'updated_at' => now(),
                ]));
            }
        }

        $moduleTypes = array_values(array_unique(array_merge(
            config('module.module_type', []),
            ['ecommerce', 'food', 'grocery', 'pharmacy', 'parcel', 'rental']
        )));

        foreach ($moduleTypes as $moduleType) {
            if (! filled($moduleType)) {
                continue;
            }

            $exists = DB::table('notification_messages')
                ->where('module_type', $moduleType)
                ->where('key', 'order_returned_message')
                ->exists();

            if (! $exists) {
                DB::table('notification_messages')->insert([
                    'key' => 'order_returned_message',
                    'module_type' => $moduleType,
                    'message' => 'Your order #{orderId} has been returned.',
                    'status' => 1,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
    }

    public function down(): void
    {
        DB::table('notification_settings')
            ->where('type', 'customer')
            ->whereIn('key', [
                'customer_order_placed',
                'customer_order_confirmed',
                'customer_order_packaging',
                'customer_order_handed_to_courier',
                'customer_order_out_for_delivery',
                'customer_order_delivered',
                'customer_order_returned',
            ])
            ->delete();

        DB::table('notification_messages')
            ->where('key', 'order_returned_message')
            ->delete();
    }
};
