<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        $now = now();

        if (Schema::hasTable('settings')) {
            $settings = [
                'tickets_notify_web' => '1',
                'tickets_notify_sms' => '1',
                'tickets_notify_whatsapp' => '0',
                'tickets_notify_on_reply' => '1',
                'tickets_notify_on_status' => '1',
            ];

            foreach ($settings as $key => $value) {
                DB::table('settings')->updateOrInsert(
                    ['key' => $key],
                    ['value' => $value, 'updated_at' => $now, 'created_at' => $now]
                );
            }
        }

        if (Schema::hasTable('sms_templates')) {
            $templates = [
                [
                    'identifier' => 'ticket_reply_customer',
                    'name' => 'Ticket Reply - Customer',
                    'body' => 'Support replied to ticket #[[ticket_id]] for order #[[order_id]]. Status: [[status]]. View: [[ticket_url]]',
                    'template_id' => null,
                    'is_active' => true,
                ],
                [
                    'identifier' => 'ticket_status_customer',
                    'name' => 'Ticket Status - Customer',
                    'body' => 'Your ticket #[[ticket_id]] status changed to [[status]]. Order #[[order_id]]. View: [[ticket_url]]',
                    'template_id' => null,
                    'is_active' => true,
                ],
            ];

            foreach ($templates as $template) {
                DB::table('sms_templates')->updateOrInsert(
                    ['identifier' => $template['identifier']],
                    array_merge($template, ['updated_at' => $now, 'created_at' => $now])
                );
            }
        }

        if (Schema::hasTable('whatsapp_templates') && Schema::hasColumn('whatsapp_templates', 'identifier')) {
            $hasLabel = Schema::hasColumn('whatsapp_templates', 'label');
            $hasActive = Schema::hasColumn('whatsapp_templates', 'is_active');

            $rows = [
                ['identifier' => 'ticket_reply_customer', 'label' => 'Ticket Reply - Customer'],
                ['identifier' => 'ticket_status_customer', 'label' => 'Ticket Status - Customer'],
            ];

            foreach ($rows as $row) {
                $payload = [
                    'identifier' => $row['identifier'],
                    'updated_at' => $now,
                    'created_at' => $now,
                ];

                if ($hasLabel) {
                    $payload['label'] = $row['label'];
                }

                if ($hasActive) {
                    $payload['is_active'] = 1;
                }

                DB::table('whatsapp_templates')->updateOrInsert(
                    ['identifier' => $row['identifier']],
                    $payload
                );
            }
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('settings')) {
            DB::table('settings')->whereIn('key', [
                'tickets_notify_web',
                'tickets_notify_sms',
                'tickets_notify_whatsapp',
                'tickets_notify_on_reply',
                'tickets_notify_on_status',
            ])->delete();
        }

        if (Schema::hasTable('sms_templates')) {
            DB::table('sms_templates')->whereIn('identifier', [
                'ticket_reply_customer',
                'ticket_status_customer',
            ])->delete();
        }

        if (Schema::hasTable('whatsapp_templates') && Schema::hasColumn('whatsapp_templates', 'identifier')) {
            DB::table('whatsapp_templates')->whereIn('identifier', [
                'ticket_reply_customer',
                'ticket_status_customer',
            ])->delete();
        }
    }
};
