<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('tenants', function (Blueprint $table) {
            $table->timestamp('trial_reminder_sent_at')->nullable()->after('trial_ends_at')->index();
            $table->timestamp('overdue_reminder_sent_at')->nullable()->after('subscription_ends_at')->index();
        });

        $settings = [
            'homepage_hero_title' => 'Run your entire Microfinance from one modern platform.',
            'homepage_hero_subtitle' => 'Smart Solution, manage savings and loans, automate repayment schedules, collect online payments, sell airtime, data and electricity bills, and get a branded customer portal.',
            'homepage_primary_cta' => 'Start 14-day trial',
            'homepage_about_title' => 'Built for lenders, cooperatives, savings groups, and community finance teams.',
            'homepage_about_body' => 'NewKipp Microfinance Solution helps Nigerian platform owners host multiple microfinance companies in one installation while keeping each tenant data, staff, branding, reports, bill payments, and billing isolated.',
            'homepage_contact_email' => 'hello@microfinance-saas.test',
            'homepage_contact_phone' => '+234 800 000 0000',
            'homepage_whatsapp_number' => '2348000000000',
            'homepage_testimonials' => json_encode([
                ['name' => 'Amina Bello', 'role' => 'Cooperative Manager', 'quote' => 'The dashboard gives us one place for savings, loans, repayments, and member records.'],
                ['name' => 'Chinedu Okafor', 'role' => 'Microfinance Owner', 'quote' => 'The branded customer portal helps our members register and apply without waiting for staff.'],
                ['name' => 'Grace Adeyemi', 'role' => 'Accountant', 'quote' => 'Receipts, ledgers, cashbook, and billing are connected. Reconciliation is easier now.'],
                ['name' => 'Tunde Balogun', 'role' => 'Branch Manager', 'quote' => 'Our loan officers can track schedules and repayments without jumping between spreadsheets.'],
                ['name' => 'Ngozi Eze', 'role' => 'Credit Lead', 'quote' => 'The KYC and guarantor records make customer review faster and more organized.'],
                ['name' => 'Sadiq Musa', 'role' => 'SaaS Operator', 'quote' => 'Multi-tenant controls let us support different companies from one cPanel-friendly install.'],
                ['name' => 'Bola Martins', 'role' => 'Operations Manager', 'quote' => 'The trial countdown and subscription controls make tenant onboarding very clear.'],
                ['name' => 'Ifeoma Nwankwo', 'role' => 'Finance Officer', 'quote' => 'The profit and loss view gives management a quick picture of business health.'],
                ['name' => 'Kunle Ajayi', 'role' => 'Field Officer', 'quote' => 'Customer self-service reduced routine calls about schedules and balances.'],
                ['name' => 'Rukayat Lawal', 'role' => 'Platform Admin', 'quote' => 'It feels built for the Nigerian market, from local payments to customer communication.'],
            ]),
            'tenant_lifecycle_email_enabled' => '1',
            'tenant_lifecycle_sms_enabled' => '0',
            'tenant_lifecycle_whatsapp_enabled' => '0',
            'tenant_lifecycle_portal_enabled' => '1',
        ];

        foreach ($settings as $key => $value) {
            DB::table('system_settings')->updateOrInsert(['key' => $key], ['value' => $value]);
        }
    }

    public function down(): void
    {
        Schema::table('tenants', function (Blueprint $table) {
            $table->dropColumn(['trial_reminder_sent_at', 'overdue_reminder_sent_at']);
        });

        DB::table('system_settings')->whereIn('key', [
            'homepage_hero_title',
            'homepage_hero_subtitle',
            'homepage_primary_cta',
            'homepage_about_title',
            'homepage_about_body',
            'homepage_contact_email',
            'homepage_contact_phone',
            'homepage_whatsapp_number',
            'homepage_testimonials',
            'tenant_lifecycle_email_enabled',
            'tenant_lifecycle_sms_enabled',
            'tenant_lifecycle_whatsapp_enabled',
            'tenant_lifecycle_portal_enabled',
        ])->delete();
    }
};
