<?php

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

return new class extends Migration
{
    private const MESSAGE = 'আপনার পণ্যটি কুরিয়ারে দেওয়ার জন্য প্রস্তুত করা হচ্ছে।';

    public function up(): void
    {
        $now = now();

        $settingExists = DB::table('notification_settings')
            ->where('key', 'customer_order_handed_to_courier')
            ->where('type', 'customer')
            ->where('module_type', 'all')
            ->exists();

        if ($settingExists) {
            DB::table('notification_settings')
                ->where('key', 'customer_order_handed_to_courier')
                ->where('type', 'customer')
                ->where('module_type', 'all')
                ->update([
                    'title' => 'customer_order_handed_to_courier',
                    'sub_title' => 'Sent_notification_when_customer_order_is_ready_for_courier',
                    'mail_status' => 'disable',
                    'sms_status' => 'active',
                    'push_notification_status' => 'active',
                    'updated_at' => $now,
                ]);
        } else {
            DB::table('notification_settings')->insert([
                'key' => 'customer_order_handed_to_courier',
                'type' => 'customer',
                'module_type' => 'all',
                'title' => 'customer_order_handed_to_courier',
                'sub_title' => 'Sent_notification_when_customer_order_is_ready_for_courier',
                'mail_status' => 'disable',
                'sms_status' => 'active',
                'push_notification_status' => 'active',
                'updated_at' => $now,
                'created_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_handover_message')
                ->exists();

            if ($exists) {
                DB::table('notification_messages')
                    ->where('module_type', $moduleType)
                    ->where('key', 'order_handover_message')
                    ->update([
                        'message' => self::MESSAGE,
                        'status' => 1,
                        'updated_at' => $now,
                    ]);

                continue;
            }

            DB::table('notification_messages')->insert([
                'module_type' => $moduleType,
                'key' => 'order_handover_message',
                'message' => self::MESSAGE,
                'status' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        }
    }

    public function down(): void
    {
        DB::table('notification_settings')
            ->where('key', 'customer_order_handed_to_courier')
            ->where('type', 'customer')
            ->where('module_type', 'all')
            ->update([
                'sms_status' => 'inactive',
                'push_notification_status' => 'inactive',
                'updated_at' => now(),
            ]);

        DB::table('notification_messages')
            ->where('key', 'order_handover_message')
            ->where('message', self::MESSAGE)
            ->update([
                'message' => 'Your order #{orderId} is being prepared for courier.',
                'updated_at' => now(),
            ]);
    }
};
