<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Login;
use App\Models\User_Detail;
use App\Models\Role;
use App\Models\Farm;
use App\Models\Farm_section;
use App\Models\Form;
use App\Models\Form_section;
use App\Models\Form_question;
use App\Models\Form_submitted;
use App\Models\Form_status;
use App\Models\Form_status_log;
use App\Models\Form_answer;
use App\Models\Form_question_option;
use Illuminate\Support\Facades\Hash;

class FormDummyData extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Seed Roles
        $roles = [
            ['name' => 'Superadmin'],
            ['name' => 'Manager'],
            ['name' => 'Farmer'],
            ['name' => 'Officer']
        ];
        foreach ($roles as $role) {
            Role::create($role);
        }

        // Seed Logins and User Details
        $logins = [
            [
                'username' => 'Superadmin',
                'password' => Hash::make('password'),
                'id_role' => Role::where('name', 'Superadmin')->first()->id,
                'user_detail' => [
                    'ic' => '123456789012',
                    'fullname' => 'Superadmin'
                ]
            ],
            [
                'username' => 'Manager',
                'password' => Hash::make('password'),
                'id_role' => Role::where('name', 'Manager')->first()->id,
                'user_detail' => [
                    'ic' => '987654321098',
                    'fullname' => 'Manager'
                ]
            ],
            [
                'username' => 'Farmer_1',
                'password' => Hash::make('password'),
                'id_role' => Role::where('name', 'Farmer')->first()->id,
                'user_detail' => [
                    'ic' => '87654328010',
                    'fullname' => 'Farmer_1'
                ]
            ],
            [
                'username' => 'Farmer_2',
                'password' => Hash::make('password'),
                'id_role' => Role::where('name', 'Farmer')->first()->id,
                'user_detail' => [
                    'ic' => '0000000000',
                    'fullname' => 'Farmer_2'
                ]
            ],
            [
                'username' => 'Officer',
                'password' => Hash::make('password'),
                'id_role' => Role::where('name', 'Officer')->first()->id,
                'user_detail' => [
                    'ic' => '11111111111',
                    'fullname' => 'Officer'
                ]
            ]

        ];

        foreach ($logins as $loginData) {
            $login = Login::create([
                'username' => $loginData['username'],
                'password' => $loginData['password'],
                'id_role' => $loginData['id_role'],
            ]);
            User_Detail::create([
                'id_login' => $login->id,
                'ic' => $loginData['user_detail']['ic'],
                'fullname' => $loginData['user_detail']['fullname'],
            ]);
        }

        // Seed Farm and Farm Sections
        $farms = [
            [
                'address' => '123 Farm Road',
                'postcode' => '12345',
                'registration_no' => 'FRM001',
                'sections' => [
                    ['name' => 'North Section', 'registration_no' => 'NS001'],
                    ['name' => 'South Section', 'registration_no' => 'SS001']
                ]
            ]
        ];

        foreach ($farms as $farmData) {
            $farm = Farm::create([
                'address' => $farmData['address'],
                'postcode' => $farmData['postcode'],
                'registration_no' => $farmData['registration_no'],
            ]);

            foreach ($farmData['sections'] as $sectionData) {
                Farm_section::create([
                    'id_farm' => $farm->id,
                    'name' => $sectionData['name'],
                    'registration_no' => $sectionData['registration_no'],
                ]);
            }
        }

        // Seed Form, Form Sections, and Form Questions
// Seed Form, Form Sections, and Form Questions
$forms = [
    [
        'title' => 'myGAP Application Checklist',
        'description' => 'Checklist for applying for myGAP certification.',
        'is_active' => true,
        'sections' => [
            [
                'title' => 'Applicant Information',
                'sequence' => 1,
                'title_numbering' => 1,
                'description' => 'Please provide your personal information.',
                'questions' => [
                    [
                        'question' => 'What is your name?',
                        'answer_type' => 'text',
                        'sequence' => 1,
                        'title_numbering' => 1.1,
                        'is_active' => true,
                    ],
                    [
                        'question' => 'Please provide your identification document (Individual)',
                        'answer_type' => 'file',
                        'sequence' => 2,
                        'title_numbering' => 1.2,
                        'is_active' => true,
                    ],
                    [
                        'question' => 'Please provide your company registration certificate (Company)',
                        'answer_type' => 'file',
                        'sequence' => 3,
                        'title_numbering' => 1.3,
                        'is_active' => true,
                    ],
                    [
                        'question' => 'Government Agency/Statutory Body',
                        'answer_type' => 'select',
                        'sequence' => 4,
                        'title_numbering' => 1.4,
                        'is_active' => true,
                        'options' => json_encode(['Yes', 'No']), // Store options as JSON
                    ]
                ]
            ],
            [
                'title' => 'Land Status',
                'sequence' => 2,
                'title_numbering' => 2,
                'description' => 'Please provide details of the land being cultivated.',
                'questions' => [
                    [
                        'question' => 'What is the land ownership status?',
                        'answer_type' => 'select',
                        'sequence' => 1,
                        'title_numbering' => 2.1,
                        'is_active' => true,
                        'options' => json_encode([
                            'Own Land', 'Rented/Pledged Land', 'LPS (Temporary Occupation License)', 
                            'TKPM (Agricultural Project Land)', 'Federal/State Agriculture Land',
                            'State-Owned Land', 'Other'
                        ]),  // Store options as JSON
                    ],
                    [
                        'question' => 'Please provide proof of land ownership (for own land)',
                        'answer_type' => 'file',
                        'sequence' => 2,
                        'title_numbering' => 2.2,
                        'is_active' => true,
                    ],
                    [
                        'question' => 'Please provide proof of land lease or pledge (for rented land)',
                        'answer_type' => 'file',
                        'sequence' => 3,
                        'title_numbering' => 2.3,
                        'is_active' => true,
                    ],
                    [
                        'question' => 'Is the land covered under an LPS (Temporary Occupation License)?',
                        'answer_type' => 'select',
                        'sequence' => 4,
                        'title_numbering' => 2.4,
                        'is_active' => true,
                        'options' => json_encode(['Yes', 'No']),  // Store options as JSON
                    ],
                    [
                        'question' => 'Is the land in the TKPM (Agricultural Project Land)?',
                        'answer_type' => 'select',
                        'sequence' => 5,
                        'title_numbering' => 2.5,
                        'is_active' => true,
                        'options' => json_encode(['Yes', 'No']),  // Store options as JSON
                    ]
                ]
            ],
            [
                'title' => 'Environmental Approval',
                'sequence' => 3,
                'title_numbering' => 3,
                'description' => 'Environmental Impact Assessment (EIA) Approval.',
                'questions' => [
                    [
                        'question' => 'Has the land been developed after 2015?',
                        'answer_type' => 'select',
                        'sequence' => 1,
                        'title_numbering' => 3.1,
                        'is_active' => true,
                        'options' => json_encode(['Yes', 'No']),  // Store options as JSON
                    ],
                    [
                        'question' => 'Please provide the EIA approval letter (if applicable).',
                        'answer_type' => 'file',
                        'sequence' => 2,
                        'title_numbering' => 3.2,
                        'is_active' => true,
                    ]
                ]
            ],
            [
                'title' => 'Farm Production',
                'sequence' => 4,
                'title_numbering' => 4,
                'description' => 'Details of farm production.',
                'questions' => [
                    [
                        'question' => 'Is the farm expected to produce crops within 6 months?',
                        'answer_type' => 'select',
                        'sequence' => 1,
                        'title_numbering' => 4.1,
                        'is_active' => true,
                        'options' => json_encode(['Yes', 'No']),  // Store options as JSON
                    ]
                ]
            ]
        ]
    ]
];

// Insert Forms, Sections, and Questions
foreach ($forms as $formData) {
    // Create the form
    $form = Form::create([
        'title' => $formData['title'],
        'description' => $formData['description'],
        'is_active' => $formData['is_active'],
    ]);

    foreach ($formData['sections'] as $sectionData) {
        // Create each section linked to the form
        $section = Form_section::create([
            'id_form' => $form->id, // Link section to the form
            'title' => $sectionData['title'],
            'sequence' => $sectionData['sequence'],
            'tittle_numbering' => $sectionData['title_numbering'],
            'description' => $sectionData['description'],
        ]);

        foreach ($sectionData['questions'] as $questionData) {
            // Create each question linked to the section and form
            $question = Form_question::create([
                'question' => $questionData['question'],
                'answer_type' => $questionData['answer_type'],
                'sequence' => $questionData['sequence'],
                'tittle_numbering' => $questionData['title_numbering'],
                'id_section' => $section->id, // Link question to the section
                'id_form' => $form->id, // Also link question to the form
                'is_active' => $questionData['is_active'],
                'options' => isset($questionData['options']) ? $questionData['options'] : null, // Handle options
            ]);
        }
    }
}



// Seed Form Status and Form Submission
$statuses = [
    ['title' => 'Submitted'],
    // Add other statuses here if needed
];
foreach ($statuses as $statusData) {
    Form_status::create($statusData);
}

// Example Form Submission Seeder
$submittedForms = [
    [
        'created_by' => 'username', // Replace with actual user logic
        'id_form' => $form->id, // Assuming the first form's ID
        'id_status' => Form_status::where('title', 'Submitted')->first()->id,
        'answers' => [
            [
                'id_form_question' => 1, // Assuming question ID for "What is your name?"
                'value' => 'John Doe',
                'remarks' => 'Answered by John Doe',
            ],
            [
                'id_form_question' => 2, // Assuming question ID for the document upload
                'value' => 'document.pdf',
                'remarks' => '',
            ],
            // Add other answers here
        ],
    ],
];

foreach ($submittedForms as $submittedFormData) {
    // Create Form Submission
    $submittedForm = Form_submitted::create([
        'created_by' => $submittedFormData['created_by'],
        'id_form' => $submittedFormData['id_form'],
        'id_status' => $submittedFormData['id_status'],
    ]);

    // Insert answers for the submitted form
    foreach ($submittedFormData['answers'] as $answerData) {
        Form_answer::create([
            'id_form_submitted' => $submittedForm->id,
            'id_form_question' => $answerData['id_form_question'],
            'value' => $answerData['value'],
            'remarks' => $answerData['remarks'],
        ]);
    }

    Form_status_log::create([
                'id_form_submitted' => $submittedForm->id,
                'created_by' => $submittedForm->created_by,
                'id_form_status' => $submittedForm->id_status,
            ]);
        }
    }
}
