<?php

namespace Database\Seeders;

use App\Models\Company;
use App\Models\Employee;
use App\Models\PayrollBranch;
use App\Models\PayrollDepartment;
use App\Models\PayrollDesignation;
use App\Models\PayrollProject;
use Illuminate\Database\Seeder;

class DemoEmployeesSeeder extends Seeder
{
    public function run(): void
    {
        $company = Company::where('company_code', 'DEMO')->first();

        if (!$company) {
            $this->command?->error('Demo Company Ltd (company_code DEMO) not found. Run php artisan db:seed first.');

            return;
        }

        $this->seedLookups($company->id);
        $created = $this->seedEmployees($company->id);

        $this->command?->info("Demo employees ready for {$company->name}: {$created} created/skipped (no payroll salary lines).");
    }

    private function seedLookups(int $companyId): void
    {
        foreach (['Accounts', 'Admin', 'Finance', 'HR', 'IT', 'Logistics', 'Marketing', 'Production', 'Sales'] as $name) {
            PayrollDepartment::withoutGlobalScopes()->firstOrCreate(
                ['company_id' => $companyId, 'name' => $name],
                ['is_active' => true],
            );
        }

        foreach (['Manager', 'Executive', 'Officer', 'Supervisor'] as $name) {
            PayrollDesignation::withoutGlobalScopes()->firstOrCreate(
                ['company_id' => $companyId, 'name' => $name],
                ['is_active' => true],
            );
        }

        foreach (['Head Office', 'Gulshan Branch'] as $name) {
            PayrollBranch::withoutGlobalScopes()->firstOrCreate(
                ['company_id' => $companyId, 'name' => $name],
                ['is_active' => true],
            );
        }

        foreach (['Core Operations', 'Expansion Project'] as $name) {
            PayrollProject::withoutGlobalScopes()->firstOrCreate(
                ['company_id' => $companyId, 'name' => $name],
                ['is_active' => true],
            );
        }
    }

    private function seedEmployees(int $companyId): int
    {
        $count = 0;

        foreach ($this->employees() as $row) {
            $displayName = trim(($row['salutation'] ? $row['salutation'] . ' ' : '') . $row['name']);

            Employee::withoutGlobalScopes()->updateOrCreate(
                ['company_id' => $companyId, 'code' => $row['code']],
                array_merge($row, [
                    'display_name'        => $displayName,
                    'name'                => $displayName,
                    'is_active'           => true,
                    'terminated_employee' => false,
                ]),
            );

            $employee = Employee::withoutGlobalScopes()
                ->where('company_id', $companyId)
                ->where('code', $row['code'])
                ->first();

            $employee?->salaryLines()->delete();

            $count++;
        }

        return $count;
    }

    /** @return list<array<string, mixed>> */
    public function employees(): array
    {
        return [
            [
                'code' => 'EMP-001', 'salutation' => 'Mr', 'name' => 'Rahim Ahmed',
                'first_name' => 'Rahim', 'last_name' => 'Ahmed', 'gender' => 'Male', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1985-03-12', 'marital_status' => 'Married',
                'blood_group' => 'B+', 'phone' => '02-55001234', 'mobile' => '01711-100001',
                'email' => 'rahim.ahmed@democompany.com', 'nid_no' => '1985123456789',
                'present_address' => 'House 12, Road 5, Gulshan-1, Dhaka-1212',
                'permanent_address' => 'Village: Dhanbari, Tangail',
                'department' => 'Accounts', 'designation' => 'Manager', 'project' => 'Core Operations',
                'branch' => 'Head Office', 'tax_id_tin' => 'TIN-100001', 'joining_date' => '2019-04-01',
                'date_of_confirmation' => '2019-10-01', 'duty_type' => 'Non Roster', 'payslip_send_method' => 'Email',
                'highest_education' => 'MBA (Finance)', 'last_organization' => 'ABC Trading Ltd',
            ],
            [
                'code' => 'EMP-002', 'salutation' => 'Ms', 'name' => 'Fatima Khan',
                'first_name' => 'Fatima', 'last_name' => 'Khan', 'gender' => 'Female', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1992-07-22', 'marital_status' => 'Single',
                'blood_group' => 'A+', 'phone' => '02-55001235', 'mobile' => '01811-100002',
                'email' => 'fatima.khan@democompany.com', 'nid_no' => '1992123456780',
                'present_address' => 'Flat 4B, Banani, Dhaka-1213',
                'permanent_address' => 'Chittagong',
                'department' => 'Accounts', 'designation' => 'Executive', 'project' => 'Core Operations',
                'branch' => 'Head Office', 'tax_id_tin' => 'TIN-100002', 'joining_date' => '2021-06-15',
                'date_of_confirmation' => '2022-01-15', 'duty_type' => 'Non Roster', 'payslip_send_method' => 'Email',
                'highest_education' => 'B.Com (Honours)', 'last_organization' => 'Prime Finance',
            ],
            [
                'code' => 'EMP-003', 'salutation' => 'Mr', 'name' => 'Karim Hassan',
                'first_name' => 'Karim', 'last_name' => 'Hassan', 'gender' => 'Male', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1988-11-05', 'marital_status' => 'Married',
                'blood_group' => 'O+', 'mobile' => '01911-100003', 'email' => 'karim.hassan@democompany.com',
                'nid_no' => '1988123456781', 'present_address' => 'Mirpur DOHS, Dhaka-1216',
                'department' => 'Admin', 'designation' => 'Officer', 'project' => 'Core Operations',
                'branch' => 'Head Office',
                'joining_date' => '2020-02-10', 'date_of_confirmation' => '2020-08-10',
                'duty_type' => 'Non Roster', 'payslip_send_method' => 'Print', 'highest_education' => 'BBA',
            ],
            [
                'code' => 'EMP-004', 'salutation' => 'Ms', 'name' => 'Nadia Islam',
                'first_name' => 'Nadia', 'last_name' => 'Islam', 'gender' => 'Female', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1990-01-18', 'marital_status' => 'Married',
                'blood_group' => 'AB+', 'mobile' => '01611-100004', 'email' => 'nadia.islam@democompany.com',
                'nid_no' => '1990123456782', 'present_address' => 'Uttara Sector 7, Dhaka-1230',
                'department' => 'Finance', 'designation' => 'Executive', 'project' => 'Core Operations',
                'branch' => 'Gulshan Branch', 'joining_date' => '2022-03-01', 'date_of_confirmation' => '2022-09-01',
                'duty_type' => 'Non Roster', 'payslip_send_method' => 'Email', 'highest_education' => 'M.Com',
            ],
            [
                'code' => 'EMP-005', 'salutation' => 'Mr', 'name' => 'Arif Hossain',
                'first_name' => 'Arif', 'last_name' => 'Hossain', 'gender' => 'Male', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1983-09-30', 'marital_status' => 'Married',
                'blood_group' => 'B-', 'mobile' => '01511-100005', 'email' => 'arif.hossain@democompany.com',
                'department' => 'Finance', 'designation' => 'Manager', 'project' => 'Expansion Project',
                'branch' => 'Head Office', 'joining_date' => '2018-01-08', 'date_of_confirmation' => '2018-07-08',
                'duty_type' => 'Non Roster', 'payslip_send_method' => 'Email', 'highest_education' => 'FCMA',
            ],
            [
                'code' => 'EMP-006', 'salutation' => 'Ms', 'name' => 'Sara Rahman',
                'first_name' => 'Sara', 'last_name' => 'Rahman', 'gender' => 'Female', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1993-04-14', 'marital_status' => 'Single',
                'blood_group' => 'A-', 'mobile' => '01712-100006', 'email' => 'sara.rahman@democompany.com',
                'department' => 'HR', 'designation' => 'Officer', 'project' => 'Core Operations',
                'branch' => 'Head Office', 'joining_date' => '2021-09-01', 'date_of_confirmation' => '2022-03-01',
                'duty_type' => 'Non Roster', 'payslip_send_method' => 'Email', 'highest_education' => 'BSS (HRM)',
            ],
            [
                'code' => 'EMP-007', 'salutation' => 'Mr', 'name' => 'Tanvir Ahmed',
                'first_name' => 'Tanvir', 'last_name' => 'Ahmed', 'gender' => 'Male', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1987-06-25', 'marital_status' => 'Married',
                'blood_group' => 'O-', 'mobile' => '01812-100007', 'email' => 'tanvir.ahmed@democompany.com',
                'department' => 'IT', 'designation' => 'Manager', 'project' => 'Core Operations',
                'branch' => 'Head Office', 'joining_date' => '2017-11-20', 'date_of_confirmation' => '2018-05-20',
                'duty_type' => 'Non Roster', 'payslip_send_method' => 'Email', 'highest_education' => 'B.Sc (CSE)',
            ],
            [
                'code' => 'EMP-008', 'salutation' => 'Ms', 'name' => 'Priya Das',
                'first_name' => 'Priya', 'last_name' => 'Das', 'gender' => 'Female', 'religion' => 'Hinduism',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1994-12-08', 'marital_status' => 'Single',
                'blood_group' => 'B+', 'mobile' => '01912-100008', 'email' => 'priya.das@democompany.com',
                'department' => 'IT', 'designation' => 'Executive', 'project' => 'Expansion Project',
                'branch' => 'Gulshan Branch', 'joining_date' => '2023-01-10', 'date_of_confirmation' => '2023-07-10',
                'duty_type' => 'Non Roster', 'payslip_send_method' => 'Email', 'highest_education' => 'B.Sc (CSE)',
            ],
            [
                'code' => 'EMP-009', 'salutation' => 'Mr', 'name' => 'Jamal Uddin',
                'first_name' => 'Jamal', 'last_name' => 'Uddin', 'gender' => 'Male', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1986-08-03', 'marital_status' => 'Married',
                'blood_group' => 'A+', 'mobile' => '01612-100009', 'email' => 'jamal.uddin@democompany.com',
                'department' => 'Logistics', 'designation' => 'Supervisor', 'project' => 'Core Operations',
                'branch' => 'Head Office', 'joining_date' => '2019-07-01', 'date_of_confirmation' => '2020-01-01',
                'duty_type' => 'Roster', 'payslip_send_method' => 'Print',
            ],
            [
                'code' => 'EMP-010', 'salutation' => 'Ms', 'name' => 'Ayesha Begum',
                'first_name' => 'Ayesha', 'last_name' => 'Begum', 'gender' => 'Female', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1991-05-17', 'marital_status' => 'Married',
                'blood_group' => 'O+', 'mobile' => '01512-100010', 'email' => 'ayesha.begum@democompany.com',
                'department' => 'Marketing', 'designation' => 'Officer', 'project' => 'Expansion Project',
                'branch' => 'Gulshan Branch', 'joining_date' => '2020-10-05', 'date_of_confirmation' => '2021-04-05',
                'duty_type' => 'Non Roster', 'payslip_send_method' => 'Email', 'highest_education' => 'BBA (Marketing)',
            ],
            [
                'code' => 'EMP-011', 'salutation' => 'Mr', 'name' => 'Rafiq Islam',
                'first_name' => 'Rafiq', 'last_name' => 'Islam', 'gender' => 'Male', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1984-02-28', 'marital_status' => 'Married',
                'blood_group' => 'B+', 'mobile' => '01713-100011', 'email' => 'rafiq.islam@democompany.com',
                'department' => 'Production', 'designation' => 'Supervisor', 'project' => 'Core Operations',
                'branch' => 'Head Office', 'joining_date' => '2016-05-15', 'date_of_confirmation' => '2016-11-15',
                'duty_type' => 'Roster', 'payslip_send_method' => 'Print',
            ],
            [
                'code' => 'EMP-012', 'salutation' => 'Mr', 'name' => 'Sohel Rana',
                'first_name' => 'Sohel', 'last_name' => 'Rana', 'gender' => 'Male', 'religion' => 'Islam',
                'nationality' => 'Bangladeshi', 'date_of_birth' => '1992-10-11', 'marital_status' => 'Single',
                'blood_group' => 'A+', 'mobile' => '01813-100012', 'email' => 'sohel.rana@democompany.com',
                'department' => 'Sales', 'designation' => 'Executive', 'project' => 'Expansion Project',
                'branch' => 'Gulshan Branch', 'joining_date' => '2022-08-20', 'date_of_confirmation' => '2023-02-20',
                'duty_type' => 'Non Roster', 'payslip_send_method' => 'Email', 'highest_education' => 'BBA',
            ],
        ];
    }
}
