<?php

declare(strict_types=1);

namespace Database\Seeders;

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

/**
 * Seeds question groups and questions for the Outsource category.
 * Creates the Outsource category if it does not already exist.
 */
final class OutsourceQuestionSeeder extends Seeder
{
    /**
     * Seed all Outsource question groups and their questions.
     */
    public function run(): void
    {
        $categoryId = DB::table('categories')->where('name', 'Outsource')->value('id');

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

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

    /**
     * Seed Group 1: Opportunity Details (not scored, no answer options, details required).
     */
    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 outsourcing opportunity is it?',
                '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' => 'List any locations included in this opportunity where we do not have a Baker Tilly firm.',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 3,
                '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' => 4,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                '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' => 5,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Who are the competitors in this space?',
                'has_yes' => false,
                'has_no' => false,
                'has_na' => false,
                'details' => 'required',
                'sort_order' => 6,
                'is_scored' => false,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 2: Client Background and History (scored, Yes/No/NA options).
     */
    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' => 'If you answer yes, you will score 1 point, if you answer no you will score 0 points',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'Have we previously worked with this client, and was the experience positive?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 1,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Have we conducted a reputational risk check on the client (negative press, ethical concerns, etc.)?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 3: Regulatory Compliance (scored, Yes/No/NA options).
     */
    private function seedRegulatoryCompliance(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Regulatory Compliance',
            'sort_order' => 3,
            'description' => null,
            'scoring_instructions' => 'If you answer yes, you will score 1 point, if you answer no you will score 0 points',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'Does the client comply with all relevant regulatory requirements?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 1,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Has the client provided complete and accurate financial records for review?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Does the client have no pending legal, tax or regulatory issues that [you know of] which could impact this opportunity?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 3,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 4: Risk Assessment (scored, Yes/No/NA options).
     */
    private function seedRiskAssessment(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Risk Assessment',
            'sort_order' => 4,
            'description' => null,
            'scoring_instructions' => 'If you answer yes, you will score 1 point, if you answer no you will score 0 points',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'Is there a clear understanding on the scope, responsibilities and deliverables?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 1,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Do we have the necessary delivery tools (platforms, technology, security measures etc.) to support this opportunity?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Have you completed a conflict check?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 3,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Can we meet the service-level agreements (SLAs) without overcommitting our resources?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 4,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Are there no special expectations or requirements from the client that may pose a challenge?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 5,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }

    /**
     * Seed Group 5: Resource Allocation (scored, Yes/No/NA options).
     */
    private function seedResourceAllocation(int $categoryId): void
    {
        $groupId = DB::table('question_groups')->insertGetId([
            'category_id' => $categoryId,
            'name' => 'Resource Allocation',
            'sort_order' => 5,
            'description' => null,
            'scoring_instructions' => 'If you answer yes, you will score 1 point, if you answer no you will score 0 points',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('questions')->insert([
            [
                'question_group_id' => $groupId,
                'text' => 'Do you have the resources required for the opportunity (personnel, time, budget)?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 1,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'question_group_id' => $groupId,
                'text' => 'Do you have the right expertise and capacity across our network to deliver high-quality service?',
                'has_yes' => true,
                'has_no' => true,
                'has_na' => true,
                'details' => 'optional',
                'sort_order' => 2,
                'is_scored' => true,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }
}
