<?php

namespace App\Domains\Leads\Actions;

use App\Domains\Conversations\Models\Conversation;
use App\Domains\Leads\Models\Lead;
use App\Domains\Leads\Models\LostReason;
use App\Domains\Leads\Models\Pipeline;
use App\Domains\Tenancy\Models\Branch;
use App\Domains\Tenancy\Models\Tenant;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class SeedDemoLeadsAction
{
    public function execute(Tenant $tenant, User $owner, Branch $branch): void
    {
        DB::transaction(function () use ($tenant, $owner, $branch) {
            if (Lead::query()->where('tenant_id', $tenant->getKey())->exists()) {
                return;
            }

            $pipeline = Pipeline::query()
                ->where('tenant_id', $tenant->getKey())
                ->where('is_default', true)
                ->with('stages')
                ->first();

            if (! $pipeline) {
                return;
            }

            $stages = $pipeline->stages->keyBy('name');

            foreach (['Too expensive', 'No reply', 'Out of stock', 'Competitor', 'Fake lead', 'Delivery issue', 'Other'] as $reason) {
                LostReason::query()->firstOrCreate([
                    'tenant_id' => $tenant->getKey(),
                    'name' => $reason,
                ], [
                    'is_active' => true,
                ]);
            }

            $conversations = Conversation::query()
                ->with('contact')
                ->where('tenant_id', $tenant->getKey())
                ->get()
                ->keyBy(fn (Conversation $conversation) => $conversation->contact?->primary_name);

            $leads = [
                ['contact' => 'Lina Saad', 'stage' => 'New Lead', 'value' => 35.00, 'title' => 'Rose Perfume Set inquiry'],
                ['contact' => 'Maya N.', 'stage' => 'Interested', 'value' => 90.00, 'title' => 'Urban Leather Bag purchase'],
                ['contact' => 'Noor Boutique', 'stage' => 'Quote Sent', 'value' => 1200.00, 'title' => 'Wholesale order request'],
            ];

            foreach ($leads as $lead) {
                /** @var Conversation|null $conversation */
                $conversation = $conversations->get($lead['contact']);
                $stage = $stages->get($lead['stage']);

                if (! $conversation || ! $stage) {
                    continue;
                }

                Lead::query()->create([
                    'tenant_id' => $tenant->getKey(),
                    'branch_id' => $branch->getKey(),
                    'contact_id' => $conversation->contact_id,
                    'conversation_id' => $conversation->getKey(),
                    'pipeline_id' => $pipeline->getKey(),
                    'stage_id' => $stage->getKey(),
                    'title' => $lead['title'],
                    'assigned_user_id' => $owner->getKey(),
                    'source_platform' => $conversation->channel_type,
                    'source_type' => $conversation->contact?->source_type,
                    'estimated_value' => $lead['value'],
                    'status' => 'active',
                ]);
            }
        });
    }
}
