<?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
    {
        if (Schema::hasTable('platform_products')) {
            return;
        }

        Schema::create('platform_products', function (Blueprint $table): void {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->string('category', 60)->default('other')->index();
            $table->string('short_description')->nullable();
            $table->text('description')->nullable();
            $table->string('website_url')->nullable();
            $table->string('signup_url')->nullable();
            $table->string('support_url')->nullable();
            $table->decimal('monthly_price', 12, 2)->nullable();
            $table->decimal('yearly_price', 12, 2)->nullable();
            $table->decimal('onboarding_commission_rate', 5, 2)->default(30);
            $table->decimal('referral_commission_rate', 5, 2)->default(10);
            $table->string('color', 20)->default('#155e75');
            $table->string('icon', 60)->nullable();
            $table->boolean('is_active')->default(true)->index();
            $table->boolean('is_featured')->default(false)->index();
            $table->unsignedInteger('sort_order')->default(0)->index();
            $table->json('settings')->nullable();
            $table->timestamps();
        });

        $now = now();
        $appUrl = rtrim((string) config('app.url'), '/');

        DB::table('platform_products')->insert([
            [
                'name' => 'Microfinance & Cooperative',
                'slug' => 'microfinance-cooperative',
                'category' => 'microfinance',
                'short_description' => 'Manage members, savings, loans, repayments, investments, bill payments, and reports.',
                'description' => 'The main microfinance and cooperative platform with tenant portals, member portal, offline/online sync, accounting, and mobile app support.',
                'website_url' => $appUrl,
                'signup_url' => $appUrl.'/register-company',
                'onboarding_commission_rate' => 30,
                'referral_commission_rate' => 10,
                'color' => '#155e75',
                'icon' => 'bi-bank',
                'is_active' => true,
                'is_featured' => true,
                'sort_order' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name' => 'Point of Sale',
                'slug' => 'point-of-sale',
                'category' => 'point_of_sale',
                'short_description' => 'Retail POS for sales, inventory, receipts, staff, branches, and daily cash reports.',
                'description' => 'Use the merchant link to onboard shops, supermarkets, pharmacies, boutiques, and distributors.',
                'website_url' => 'https://pos.example.com',
                'signup_url' => 'https://pos.example.com/register',
                'onboarding_commission_rate' => 30,
                'referral_commission_rate' => 10,
                'color' => '#dc2626',
                'icon' => 'bi-receipt',
                'is_active' => true,
                'is_featured' => true,
                'sort_order' => 2,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name' => 'Hotel Management',
                'slug' => 'hotel-management',
                'category' => 'hotel',
                'short_description' => 'Run bookings, rooms, guests, restaurant orders, payments, and occupancy reports.',
                'description' => 'Designed for hotels, guest houses, short-let apartments, resorts, and event lodges.',
                'website_url' => 'https://hotel.example.com',
                'signup_url' => 'https://hotel.example.com/register',
                'onboarding_commission_rate' => 30,
                'referral_commission_rate' => 10,
                'color' => '#7c3aed',
                'icon' => 'bi-building',
                'is_active' => true,
                'is_featured' => true,
                'sort_order' => 3,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name' => 'HR & Payroll',
                'slug' => 'hr-payroll',
                'category' => 'hr_payroll',
                'short_description' => 'Manage staff records, attendance, payroll, leave, departments, and HR reports.',
                'description' => 'A practical HR platform for SMEs, schools, hospitals, hotels, factories, and service companies.',
                'website_url' => 'https://hr.example.com',
                'signup_url' => 'https://hr.example.com/register',
                'onboarding_commission_rate' => 30,
                'referral_commission_rate' => 10,
                'color' => '#2563eb',
                'icon' => 'bi-people',
                'is_active' => true,
                'is_featured' => true,
                'sort_order' => 4,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name' => 'Hospital & Clinic',
                'slug' => 'hospital-clinic',
                'category' => 'hospital',
                'short_description' => 'Patient registration, appointments, billing, pharmacy, lab, and clinical records.',
                'description' => 'Built for hospitals, clinics, labs, pharmacies, and diagnostic centres.',
                'website_url' => 'https://hospital.example.com',
                'signup_url' => 'https://hospital.example.com/register',
                'onboarding_commission_rate' => 30,
                'referral_commission_rate' => 10,
                'color' => '#16a34a',
                'icon' => 'bi-hospital',
                'is_active' => true,
                'is_featured' => true,
                'sort_order' => 5,
                'created_at' => $now,
                'updated_at' => $now,
            ],
        ]);
    }

    public function down(): void
    {
        Schema::dropIfExists('platform_products');
    }
};
