<?php

namespace Modules\CRMCore\Database\Seeders;

use App\Models\ModuleSetting;
use Illuminate\Database\Seeder;

class CRMCoreSettingsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * Settings keys must match those defined in CRMCoreSettingsHandler
     * and used by CRMValidationService, CRMNotificationService, etc.
     */
    public function run(): void
    {
        $settings = [
            // Company Management Settings
            'auto_generate_company_codes' => true,
            'company_code_prefix' => 'COM',
            'company_code_start_number' => '1000',
            'enable_company_industry_categories' => true,
            'require_company_assignment' => false,
            'default_company_industry' => '',

            // Contact Management Settings
            'auto_generate_contact_codes' => false,
            'contact_code_prefix' => 'CNT',
            'contact_code_start_number' => '1000',
            'enable_contact_duplicate_detection' => true,
            'duplicate_detection_fields' => 'email_and_phone',
            'require_company_for_contacts' => false,
            'require_contact_assignment' => false,

            // Customer Management Settings
            'auto_generate_customer_codes' => true,
            'customer_code_prefix' => 'CUS',
            'customer_code_start_number' => '1000',
            'auto_create_customer_on_first_purchase' => true,
            'default_credit_limit' => '1000',
            'default_payment_terms' => 'cod',
            'default_customer_group_id' => null,
            'default_customer_active_status' => true,
            'enable_customer_duplicate_detection' => true,

            // Lead Management Settings
            'auto_generate_lead_codes' => true,
            'lead_code_prefix' => 'LD',
            'lead_code_start_number' => '1000',
            'require_lead_assignment' => true,
            'lead_auto_convert_days' => '30',
            'default_lead_source_id' => null,
            'default_lead_status_id' => null,

            // Deal Management Settings
            'auto_generate_deal_codes' => true,
            'deal_code_prefix' => 'DL',
            'deal_code_start_number' => '1000',
            'deal_auto_close_days' => '90',
            'require_deal_assignment' => true,
            'default_deal_probability' => '10',
            'default_pipeline_id' => null,

            // Task Management Settings
            'auto_generate_task_codes' => false,
            'task_code_prefix' => 'TSK',
            'task_code_start_number' => '1000',
            'task_reminder_enabled' => true,
            'task_reminder_before_hours' => '24',
            'require_task_assignment' => true,
            'enable_task_time_tracking' => true,
            'default_task_priority_id' => null,
            'default_task_status_id' => null,

            // Notification Settings
            'notify_lead_assignment' => true,
            'notify_deal_stage_change' => true,
            'notify_task_due' => true,

            // Display Settings
            'items_per_page' => '25',
            'enable_kanban_view' => true,
            'show_activity_timeline' => true,
        ];

        foreach ($settings as $key => $value) {
            ModuleSetting::firstOrCreate(
                [
                    'module' => 'CRMCore',
                    'key' => $key,
                ],
                [
                    'value' => is_bool($value) ? ($value ? '1' : '0') : ($value === null ? '' : (string) $value),
                    'created_at' => now(),
                    'updated_at' => now(),
                ]
            );
        }
    }
}
