<?php

namespace Modules\MedPortal\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Schema;
use Modules\MedPortal\Entities\MportalQuestionnaire;
use Modules\MedPortal\Services\QuestionnaireService;

/**
 * Seeds the two SYSTEM questionnaires (business_id NULL): the standard PHQ-9 and
 * GAD-7 instruments — items + Likert options + scoring bands. Idempotent via
 * updateOrCreate keyed on (business_id NULL, code). Available to every business
 * through MportalQuestionnaire::availableTo().
 *
 * PHQ-9 / GAD-7 use the standard 4-point Likert (0=Not at all … 3=Nearly every
 * day) over the recall period "the last 2 weeks".
 */
class MedportalQuestionnaireSeeder extends Seeder
{
    /** Shared 4-point Likert options for PHQ-9 and GAD-7. */
    private function likert(): array
    {
        return [
            ['label' => 'Not at all', 'value' => 0],
            ['label' => 'Several days', 'value' => 1],
            ['label' => 'More than half the days', 'value' => 2],
            ['label' => 'Nearly every day', 'value' => 3],
        ];
    }

    public function run(): void
    {
        if (! Schema::hasTable('mportal_questionnaires')) {
            return;
        }

        $likert = $this->likert();

        // ── PHQ-9 (Patient Health Questionnaire — depression, 9 items, 0–27) ──
        $phq9Questions = [
            'Little interest or pleasure in doing things',
            'Feeling down, depressed, or hopeless',
            'Trouble falling or staying asleep, or sleeping too much',
            'Feeling tired or having little energy',
            'Poor appetite or overeating',
            'Feeling bad about yourself — or that you are a failure or have let yourself or your family down',
            'Trouble concentrating on things, such as reading the newspaper or watching television',
            'Moving or speaking so slowly that other people could have noticed — or the opposite, being fidgety or restless',
            'Thoughts that you would be better off dead, or of hurting yourself in some way',
        ];

        MportalQuestionnaire::updateOrCreate(
            ['business_id' => null, 'code' => 'phq9'],
            [
                'title'     => 'PHQ-9 (Depression)',
                'items'     => array_map(fn ($q) => ['q' => $q, 'options' => $likert], $phq9Questions),
                'scoring'   => [
                    'max_score' => 27,
                    'bands'     => QuestionnaireService::PHQ9_BANDS,
                ],
                'is_active' => true,
            ]
        );

        // ── GAD-7 (Generalized Anxiety Disorder — 7 items, 0–21) ──
        $gad7Questions = [
            'Feeling nervous, anxious, or on edge',
            'Not being able to stop or control worrying',
            'Worrying too much about different things',
            'Trouble relaxing',
            'Being so restless that it is hard to sit still',
            'Becoming easily annoyed or irritable',
            'Feeling afraid, as if something awful might happen',
        ];

        MportalQuestionnaire::updateOrCreate(
            ['business_id' => null, 'code' => 'gad7'],
            [
                'title'     => 'GAD-7 (Anxiety)',
                'items'     => array_map(fn ($q) => ['q' => $q, 'options' => $likert], $gad7Questions),
                'scoring'   => [
                    'max_score' => 21,
                    'bands'     => QuestionnaireService::GAD7_BANDS,
                ],
                'is_active' => true,
            ]
        );

        if ($this->command) {
            $this->command->info('MedPortal: PHQ-9 and GAD-7 system questionnaires seeded.');
        }
    }
}
