<?php

namespace App\Modules\HR\Recruitment\Services;

use App\Modules\HR\Recruitment\Support\OnboardingAccountType;
use App\Modules\HR\Recruitment\Support\OnboardingSection;
use App\Modules\HR\Recruitment\Support\OnboardingTaskStatus;

class OnboardingBootstrapService
{
    /** @return list<array<string, mixed>> */
    public function defaultTasks(): array
    {
        $n = 0;
        $items = [];
        foreach ($this->sections() as $section => $rows) {
            foreach ($rows as $row) {
                $items[] = [
                    'section' => $section,
                    'title' => $row['title'],
                    'description' => $row['description'] ?? null,
                    'status' => OnboardingTaskStatus::PENDING,
                    'is_required' => $row['required'] ?? true,
                    'sort_order' => ++$n,
                    'meta' => $row['meta'] ?? null,
                ];
            }
        }

        return $items;
    }

    /** @return array<string, list<array<string, mixed>>> */
    private function sections(): array
    {
        return [
            OnboardingSection::CHECKLIST => [
                ['title' => 'Welcome pack issued'],
                ['title' => 'HR file opened'],
                ['title' => 'Emergency contacts collected'],
                ['title' => 'Bank details verified', 'required' => false],
            ],
            OnboardingSection::DOCUMENTS => [
                ['title' => 'National ID / Passport copy', 'meta' => ['document_hint' => 'national_id_front']],
                ['title' => 'Signed employment contract', 'meta' => ['document_hint' => 'contract']],
                ['title' => 'CV archived', 'meta' => ['document_hint' => 'cv'], 'required' => false],
                ['title' => 'Certificates uploaded', 'meta' => ['document_hint' => 'certificate'], 'required' => false],
            ],
            OnboardingSection::TRAINING => [
                ['title' => 'Company policies training'],
                ['title' => 'Health & safety briefing'],
                ['title' => 'Role-specific training'],
            ],
            OnboardingSection::EQUIPMENT => [
                ['title' => 'Laptop / workstation', 'meta' => ['asset_label' => 'laptop']],
                ['title' => 'Access badge / keys', 'meta' => ['asset_label' => 'badge']],
                ['title' => 'Phone / headset', 'meta' => ['asset_label' => 'phone'], 'required' => false],
            ],
            OnboardingSection::EMAIL => [
                ['title' => 'Company email mailbox created'],
                ['title' => 'Email signature configured'],
                ['title' => 'Distribution lists joined', 'required' => false],
            ],
            OnboardingSection::ACCOUNTS => $this->accountRows(),
            OnboardingSection::ORIENTATION => [
                ['title' => 'Meet the team'],
                ['title' => 'Workspace tour'],
                ['title' => 'Manager 1:1 scheduled'],
                ['title' => 'First-week goals set', 'required' => false],
            ],
        ];
    }

    /** @return list<array<string, mixed>> */
    private function accountRows(): array
    {
        $map = [
            OnboardingAccountType::ERP => 'ERP account',
            OnboardingAccountType::MICROSOFT_365 => 'Microsoft 365',
            OnboardingAccountType::EMAIL => 'Email account',
            OnboardingAccountType::VPN => 'VPN access',
            OnboardingAccountType::GIT => 'Git access',
            OnboardingAccountType::SLACK => 'Slack workspace',
            OnboardingAccountType::CUSTOM => 'Custom account',
        ];
        $optional = [
            OnboardingAccountType::GIT,
            OnboardingAccountType::SLACK,
            OnboardingAccountType::CUSTOM,
        ];
        $out = [];
        foreach ($map as $type => $title) {
            $out[] = [
                'title' => $title,
                'meta' => ['account_type' => $type],
                'required' => ! in_array($type, $optional, true),
            ];
        }

        return $out;
    }
}
