<?php

namespace Modules\CRMCore\Database\Seeders;

use Illuminate\Database\Seeder;
use Modules\CRMCore\App\Models\DealPipeline;
use Modules\CRMCore\App\Models\DealStage;
use Modules\CRMCore\App\Models\LeadSource;
use Modules\CRMCore\App\Models\LeadStatus;

class CRMCoreSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->seedLeadStatuses();
        $this->seedLeadSources();
        $this->seedDealPipelines();
    }

    /**
     * Seed lead statuses for the Kanban board
     */
    private function seedLeadStatuses(): void
    {
        $statuses = [
            ['name' => 'New', 'color' => '#6c757d', 'position' => 1, 'is_default' => true, 'is_final' => false],
            ['name' => 'Contacted', 'color' => '#17a2b8', 'position' => 2, 'is_default' => false, 'is_final' => false],
            ['name' => 'Qualified', 'color' => '#ffc107', 'position' => 3, 'is_default' => false, 'is_final' => false],
            ['name' => 'Proposal', 'color' => '#007bff', 'position' => 4, 'is_default' => false, 'is_final' => false],
            ['name' => 'Negotiation', 'color' => '#fd7e14', 'position' => 5, 'is_default' => false, 'is_final' => false],
            ['name' => 'Won', 'color' => '#28a745', 'position' => 6, 'is_default' => false, 'is_final' => true],
            ['name' => 'Lost', 'color' => '#dc3545', 'position' => 7, 'is_default' => false, 'is_final' => true],
        ];

        foreach ($statuses as $status) {
            LeadStatus::firstOrCreate(
                ['name' => $status['name']],
                $status
            );
        }
    }

    /**
     * Seed lead sources
     */
    private function seedLeadSources(): void
    {
        $sources = [
            ['name' => 'Website', 'is_active' => true],
            ['name' => 'Referral', 'is_active' => true],
            ['name' => 'Social Media', 'is_active' => true],
            ['name' => 'Cold Call', 'is_active' => true],
            ['name' => 'Email Campaign', 'is_active' => true],
            ['name' => 'Trade Show', 'is_active' => true],
            ['name' => 'Partner', 'is_active' => true],
            ['name' => 'Advertisement', 'is_active' => true],
            ['name' => 'Other', 'is_active' => true],
        ];

        foreach ($sources as $source) {
            LeadSource::firstOrCreate(
                ['name' => $source['name']],
                $source
            );
        }
    }

    /**
     * Seed deal pipelines and stages
     */
    private function seedDealPipelines(): void
    {
        // Create default sales pipeline
        $pipeline = DealPipeline::firstOrCreate(
            ['name' => 'Sales Pipeline'],
            [
                'name' => 'Sales Pipeline',
                'description' => 'Default sales pipeline for managing deals',
                'is_default' => true,
                'is_active' => true,
                'position' => 1,
            ]
        );

        // Create stages for the pipeline
        $stages = [
            ['name' => 'Qualification', 'color' => '#6c757d', 'position' => 1, 'is_default_for_pipeline' => true, 'is_won_stage' => false, 'is_lost_stage' => false],
            ['name' => 'Needs Analysis', 'color' => '#17a2b8', 'position' => 2, 'is_default_for_pipeline' => false, 'is_won_stage' => false, 'is_lost_stage' => false],
            ['name' => 'Value Proposition', 'color' => '#ffc107', 'position' => 3, 'is_default_for_pipeline' => false, 'is_won_stage' => false, 'is_lost_stage' => false],
            ['name' => 'Proposal', 'color' => '#007bff', 'position' => 4, 'is_default_for_pipeline' => false, 'is_won_stage' => false, 'is_lost_stage' => false],
            ['name' => 'Negotiation', 'color' => '#fd7e14', 'position' => 5, 'is_default_for_pipeline' => false, 'is_won_stage' => false, 'is_lost_stage' => false],
            ['name' => 'Closed Won', 'color' => '#28a745', 'position' => 6, 'is_default_for_pipeline' => false, 'is_won_stage' => true, 'is_lost_stage' => false],
            ['name' => 'Closed Lost', 'color' => '#dc3545', 'position' => 7, 'is_default_for_pipeline' => false, 'is_won_stage' => false, 'is_lost_stage' => true],
        ];

        foreach ($stages as $stage) {
            DealStage::firstOrCreate(
                ['pipeline_id' => $pipeline->id, 'name' => $stage['name']],
                array_merge($stage, ['pipeline_id' => $pipeline->id])
            );
        }
    }
}
