<?php

declare(strict_types=1);

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

/**
 * Seeds question groups and questions for the Tax category.
 * Assumes the Tax category is created if not already present.
 */
final class TaxQuestionSeeder extends Seeder
{
    /**
     * Seed all Tax question groups and their questions.
     */
    public function run(): void
    {
        $categoryId = DB::table('categories')->where('name', 'Tax')->value('id');

        if ($categoryId === null) {
            $categoryId = DB::table('categories')->insertGetId([
                'name' => 'Tax',
                'sort_order' => 6,
                'created_at' => now(),
                'updated_at' => now(),
            ]);
        }

        $this->seedOpportunityDetails($categoryId);
        $this->seedClientBackgroundAndHistory($categoryId);
        $this->seedFinancialInformation($categoryId);
        $this->seedRegulatoryCompliance($categoryId);
        $this->seedRiskAssessment($categoryId);
        $this->seedResourceAllocation($categoryId);
        $this->seedStakeholderEngagement($categoryId);
    }

    /**
     * Seed Group 1: Opportunity Details (mixed — some scored with Yes/No, some text-only).
     */
    private function seedOpportunityDetails(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Opportunity Details',
            'sort_order' => 1,
            'description' => null,
            'scoring_instructions' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'What sort of opportunity is it?/Describe the Scope of Work',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 1,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'How many locations involved in this opportunity?',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 2,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Do we have a Baker Tilly firm in all locations within this opportunity?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'req_on_no',
                'sort_order' => 3,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Has the client requested any additional information from our firms?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 4,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'What is the deadline to respond to the client on this opportunity?',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 5,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Where is the client HQ?',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 6,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Who is the competition?',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 7,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 2: Client Background and History (mixed — Q1 text-only, Q2/Q3 scored Yes/No).
     */
    private function seedClientBackgroundAndHistory(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Client Background and History',
            'sort_order' => 2,
            'description' => null,
            'scoring_instructions' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'What is the client\'s business and industry?',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 1,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Have there been any significant changes in the client\'s business operations or structure recently?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'req_on_yes',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Is the client an existing client?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 3,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 3: Financial Information (scored, Yes/No options).
     */
    private function seedFinancialInformation(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Financial Information',
            'sort_order' => 3,
            'description' => null,
            'scoring_instructions' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'Has the client provided enough financial information about their company?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'optional',
                'sort_order' => 1,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Are there any significant financial risks or uncertainties that you are aware of?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'req_on_yes',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 4: Regulatory Compliance (scored, Yes/No options).
     */
    private function seedRegulatoryCompliance(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Regulatory Compliance',
            'sort_order' => 4,
            'description' => null,
            'scoring_instructions' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'Does the client comply with all relevant regulatory requirements and standards?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'req_on_no',
                'sort_order' => 1,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Are there any pending legal or regulatory issues that you know of that could impact the opportunity?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'req_on_yes',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Has the client been subject to any regulatory investigations or penalties?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'req_on_yes',
                'sort_order' => 3,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 5: Risk Assessment (scored, Yes/No options).
     */
    private function seedRiskAssessment(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Risk Assessment',
            'sort_order' => 5,
            'description' => null,
            'scoring_instructions' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'Are there any potential risks or challenges associated with the opportunity?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'req_on_yes',
                'sort_order' => 1,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Are there any potential conflicts of interest?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'req_on_yes',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 6: Resource Allocation (mixed — Q1/Q4 text-only, Q2/Q3 scored Yes/No).
     */
    private function seedResourceAllocation(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Resource Allocation',
            'sort_order' => 6,
            'description' => null,
            'scoring_instructions' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'What resources are required for the opportunity (personnel, time, budget)?',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'optional',
                'sort_order' => 1,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Are there any constraints on the availability of your resources?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'optional',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Do you know of the any constraints on the availability of other firms included in this opportunity?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'optional',
                'sort_order' => 3,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'What is the expected timeline for the opportunity, including any critical deadlines that must be met?',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'optional',
                'sort_order' => 4,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 7: Stakeholder Engagement (mixed — Q1 text-only, Q2 scored Yes/No).
     */
    private function seedStakeholderEngagement(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Stakeholder Engagement',
            'sort_order' => 7,
            'description' => null,
            'scoring_instructions' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'Who are the key stakeholders involved in this opportunity?',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 1,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Are there any special expectations and requirements?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => false,
                'details' => 'optional',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }
}
