<?php

require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

use Illuminate\Support\Facades\DB;

echo "Seeding User Categories and User Types...\n\n";

// User Categories
$categories = [
    ['name' => 'Client', 'description' => 'Regular clients using the system'],
    ['name' => 'Professional', 'description' => 'Immigration professionals and consultants'],
    ['name' => 'Student', 'description' => 'Students using the learning management system'],
    ['name' => 'Administrator', 'description' => 'System administrators'],
];

echo "Seeding User Categories:\n";
foreach ($categories as $category) {
    $existing = DB::table('user_categories')->where('name', $category['name'])->first();
    if (!$existing) {
        DB::table('user_categories')->insert([
            'name' => $category['name'],
            'description' => $category['description'],
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        echo "  ✓ Created category: {$category['name']}\n";
    } else {
        echo "  - Category already exists: {$category['name']}\n";
    }
}

// Get category IDs
$clientCat = DB::table('user_categories')->where('name', 'Client')->first();
$professionalCat = DB::table('user_categories')->where('name', 'Professional')->first();
$studentCat = DB::table('user_categories')->where('name', 'Student')->first();
$adminCat = DB::table('user_categories')->where('name', 'Administrator')->first();

// User Types
$types = [
    // Client types
    ['name' => 'Individual Client', 'code' => 'individual_client', 'description' => 'Individual seeking immigration services', 'user_category_id' => $clientCat->id],
    ['name' => 'Corporate Client', 'code' => 'corporate_client', 'description' => 'Corporate entity seeking immigration services', 'user_category_id' => $clientCat->id],
    ['name' => 'Family Client', 'code' => 'family_client', 'description' => 'Family unit seeking immigration services', 'user_category_id' => $clientCat->id],
    
    // Professional types
    ['name' => 'RCIC', 'code' => 'rcic', 'description' => 'Regulated Canadian Immigration Consultant', 'user_category_id' => $professionalCat->id],
    ['name' => 'Immigration Lawyer', 'code' => 'immigration_lawyer', 'description' => 'Licensed immigration lawyer', 'user_category_id' => $professionalCat->id],
    ['name' => 'Paralegal', 'code' => 'paralegal', 'description' => 'Immigration paralegal', 'user_category_id' => $professionalCat->id],
    ['name' => 'Case Manager', 'code' => 'case_manager', 'description' => 'Immigration case manager', 'user_category_id' => $professionalCat->id],
    
    // Student types
    ['name' => 'RCIC Student', 'code' => 'rcic_student', 'description' => 'Student studying to become RCIC', 'user_category_id' => $studentCat->id],
    ['name' => 'Law Student', 'code' => 'law_student', 'description' => 'Law student specializing in immigration', 'user_category_id' => $studentCat->id],
    ['name' => 'Course Participant', 'code' => 'course_participant', 'description' => 'General course participant', 'user_category_id' => $studentCat->id],
    
    // Administrator types
    ['name' => 'Super Admin', 'code' => 'super_admin', 'description' => 'Full system administrator', 'user_category_id' => $adminCat->id],
    ['name' => 'Portal Admin', 'code' => 'portal_admin', 'description' => 'Portal administrator', 'user_category_id' => $adminCat->id],
    ['name' => 'Content Manager', 'code' => 'content_manager', 'description' => 'Content management administrator', 'user_category_id' => $adminCat->id],
];

echo "\nSeeding User Types:\n";
foreach ($types as $type) {
    $existing = DB::table('user_types')->where('code', $type['code'])->first();
    if (!$existing) {
        DB::table('user_types')->insert([
            'name' => $type['name'],
            'code' => $type['code'],
            'description' => $type['description'],
            'user_category_id' => $type['user_category_id'],
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        echo "  ✓ Created type: {$type['name']} ({$type['code']})\n";
    } else {
        echo "  - Type already exists: {$type['name']}\n";
    }
}

echo "\n========================================\n";
echo "✓ Seeding complete!\n";
echo "========================================\n";

// Show summary
$catCount = DB::table('user_categories')->count();
$typeCount = DB::table('user_types')->count();
echo "Total User Categories: $catCount\n";
echo "Total User Types: $typeCount\n";
