<?php

namespace Database\Seeders;

use App\Models\Announcement;
use App\Models\AttendanceRecord;
use App\Models\Department;
use App\Models\Designation;
use App\Models\EmployeeDocument;
use App\Models\HrTicket;
use App\Models\JobPosting;
use App\Models\LeaveRequest;
use App\Models\LeaveType;
use App\Models\Payroll;
use App\Models\PerformanceReview;
use App\Models\Training;
use App\Models\TrainingEnrollment;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $operations = Department::create(['name' => 'Operations', 'code' => 'OPS', 'description' => 'Field operations and service delivery', 'status' => 'active']);
        $hr = Department::create(['name' => 'Human Resources', 'code' => 'HR', 'description' => 'People operations and compliance', 'status' => 'active']);
        $finance = Department::create(['name' => 'Finance', 'code' => 'FIN', 'description' => 'Payroll, accounting and reporting', 'status' => 'active']);

        $manager = Designation::create(['name' => 'Manager', 'level' => 'M1', 'description' => 'Department manager', 'status' => 'active']);
        $officer = Designation::create(['name' => 'Officer', 'level' => 'O1', 'description' => 'Operational staff', 'status' => 'active']);
        $coordinator = Designation::create(['name' => 'Coordinator', 'level' => 'C1', 'description' => 'Team coordination role', 'status' => 'active']);

        $admin = User::create([
            'staff_id' => 'HR-ADMIN-001',
            'name' => 'Movex HR Admin',
            'email' => 'hradmin@movex-ng.test',
            'password' => Hash::make('password123'),
            'role' => 'admin',
            'phone' => '+2348000000001',
            'department_id' => $hr->id,
            'designation_id' => $manager->id,
            'job_title' => 'HR Manager',
            'employment_date' => now()->subYears(2)->toDateString(),
            'employment_status' => 'active',
            'basic_salary' => 750000,
            'bank_name' => 'GTBank',
            'bank_account' => '0001112223',
        ]);

        $users = collect([
            User::create([
                'staff_id' => 'HR-USER-001', 'name' => 'Chinedu Okafor', 'email' => 'chinedu@movex-ng.test', 'password' => Hash::make('password123'), 'role' => 'user', 'phone' => '+2348000000002', 'department_id' => $operations->id, 'designation_id' => $coordinator->id, 'job_title' => 'Regional Coordinator', 'employment_date' => now()->subMonths(10)->toDateString(), 'employment_status' => 'active', 'basic_salary' => 420000, 'bank_name' => 'Zenith Bank', 'bank_account' => '0002223334',
            ]),
            User::create([
                'staff_id' => 'HR-USER-002', 'name' => 'Amina Bello', 'email' => 'amina@movex-ng.test', 'password' => Hash::make('password123'), 'role' => 'user', 'phone' => '+2348000000003', 'department_id' => $finance->id, 'designation_id' => $officer->id, 'job_title' => 'Account Officer', 'employment_date' => now()->subMonths(7)->toDateString(), 'employment_status' => 'active', 'basic_salary' => 360000, 'bank_name' => 'Access Bank', 'bank_account' => '0003334445',
            ]),
            User::create([
                'staff_id' => 'HR-USER-003', 'name' => 'Tunde Adeyemi', 'email' => 'tunde@movex-ng.test', 'password' => Hash::make('password123'), 'role' => 'user', 'phone' => '+2348000000004', 'department_id' => $operations->id, 'designation_id' => $officer->id, 'job_title' => 'State Coordinator', 'employment_date' => now()->subMonths(5)->toDateString(), 'employment_status' => 'probation', 'basic_salary' => 330000, 'bank_name' => 'UBA', 'bank_account' => '0004445556',
            ]),
        ]);

        foreach (['Annual Leave' => 21, 'Sick Leave' => 10, 'Maternity Leave' => 90, 'Paternity Leave' => 14, 'Compassionate Leave' => 5] as $name => $days) {
            LeaveType::create(['name' => $name, 'annual_days' => $days, 'requires_document' => in_array($name, ['Sick Leave', 'Maternity Leave']), 'status' => 'active']);
        }

        $annual = LeaveType::where('name', 'Annual Leave')->first();
        LeaveRequest::create(['user_id' => $users[0]->id, 'leave_type_id' => $annual->id, 'start_date' => now()->addDays(8)->toDateString(), 'end_date' => now()->addDays(12)->toDateString(), 'total_days' => 5, 'reason' => 'Family engagement', 'status' => 'pending']);

        foreach ($users as $user) {
            AttendanceRecord::create(['user_id' => $user->id, 'work_date' => today()->toDateString(), 'clock_in_at' => now()->setTime(8, 30), 'clock_out_at' => null, 'status' => 'present', 'approved_by' => $admin->id]);
            Payroll::create(['user_id' => $user->id, 'pay_period_start' => now()->startOfMonth()->toDateString(), 'pay_period_end' => now()->endOfMonth()->toDateString(), 'basic_salary' => $user->basic_salary, 'allowances' => 50000, 'deductions' => 10000, 'tax' => 15000, 'net_pay' => $user->basic_salary + 50000 - 25000, 'status' => 'approved']);
            EmployeeDocument::create(['user_id' => $user->id, 'title' => 'Employment Letter', 'document_type' => 'Contract', 'status' => 'valid', 'notes' => 'Seeded employment record']);
        }

        PerformanceReview::create(['user_id' => $users[0]->id, 'review_period' => 'Q1 2026', 'score' => 4, 'summary' => 'Strong coordination and timely reporting.', 'strengths' => 'Communication and ownership', 'improvement_plan' => 'Improve documentation discipline', 'status' => 'completed', 'reviewed_by' => $admin->id, 'reviewed_at' => now()]);

        Announcement::create(['title' => 'Welcome to Movex Nigeria HR CRM', 'body' => 'All staff should update their profile, bank details and emergency contact information before payroll cut-off.', 'audience' => 'all', 'status' => 'published', 'published_at' => now(), 'created_by' => $admin->id]);

        HrTicket::create(['user_id' => $users[1]->id, 'subject' => 'Payslip clarification', 'category' => 'payroll', 'priority' => 'normal', 'status' => 'open', 'message' => 'Please confirm my allowance breakdown for this month.']);

        $training = Training::create(['title' => 'Workplace Safety and HR Policy Orientation', 'description' => 'Internal onboarding for staff conduct, reporting lines and workplace safety.', 'start_date' => now()->addDays(14)->toDateString(), 'end_date' => now()->addDays(14)->toDateString(), 'location' => 'Lagos Office', 'status' => 'active', 'created_by' => $admin->id]);
        TrainingEnrollment::create(['training_id' => $training->id, 'user_id' => $users[0]->id, 'status' => 'registered']);

        JobPosting::create(['title' => 'HR Officer', 'department_id' => $hr->id, 'employment_type' => 'full_time', 'location' => 'Lagos, Nigeria', 'description' => 'Support recruitment, staff records and HR operations.', 'requirements' => 'Minimum OND/HND/BSc with HR experience.', 'status' => 'open', 'closing_date' => now()->addMonth()->toDateString(), 'created_by' => $admin->id]);
    }
}
