﻿<?php

namespace App\Imports;

use App\Models\Branch;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use App\Models\User;
use App\Models\Employee;
use App\Models\Profile;
use App\Models\JobInfo;
use App\Models\BankInfo;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class EmployeesImport implements ToModel, WithHeadingRow
{
    use Importable;
    function convertExcelDate($value)
    {
        if (is_numeric($value)) {
            return Date::excelToDateTimeObject($value)->format('Y-m-d');
        }
        return $value;
    }
    public function model(array $row)
    {
        // Skip empty rows
        if (empty(array_filter($row))) {
            return null;
        }

        // Create User
        $user = User::create([
            'email' => $row['email'],
            'password' => Hash::make('password'),
            'invitation_code' => Str::random(22), // Random 22-character code
            'is_active' => 0,
            'is_registered' => 0,
        ]);
        $branch = null;
        if (isset($row['branch'])) {
            $branch = Branch::updateOrCreate(
                [
                    'name' => $row['branch'],
                    'created_by' => auth()->user()->creatorId(),
                ],
                [
                    'name' => $row['branch'],
                ]
            );
        }

        // Create Profile
        Profile::create([
            'user_id' => $user->id,
            'first_name' => $row['first_name'] ?? null,
            'last_name' => $row['last_name'] ?? null,
            'middle_name' => $row['middle_name'] ?? null,
            'dob' => isset($row['dob']) ? $this->convertExcelDate($row['dob']) : null,
            'work_phone' => $row['work_phone'] ?? null,
            'mobile_phone' => $row['mobile_phone'] ?? null,
            'work_email' => $row['work_email'] ?? null,
            'personal_email' => $row['email'],
            'address' => $row['address'] ?? null,
            'nationality' => $row['nationality'] ?? null,
            'gender' => $row['gender'] ?? null,
            'marital_status' => $row['marital_status'] ?? null,
        ]);

        // Create Job Info

        $jobData = [
            'user_id' => $user->id,
            'employee_id' => $row['employee_id'],
            'employment_type_id' => $row['employment_type_id'],
            'pay_frequency' => $row['pay_frequency'],
            'gross_amount' => $row['gross_salary_amount'] ?? 0,
            'annual_rent_amount' => $row['annual_rent_amount'] ?? 0.00,
            'basic_salary_percentage_on_gross' => $row['percentage_basic_salary_on_gross'],
            'currency' => $row['currency'] ?? "NGN",
            'hire_date' => isset($row['employment_date']) ? $this->convertExcelDate($row['employment_date']) : null,
            'is_annual_pay' => isset($row['is_annual_pay']) && strtolower($row['is_annual_pay']) == 'yes'
        ];
        JobInfo::create($jobData);

        // Create Bank Info
        BankInfo::create([
            'user_id' => $user->id,
            'bank_name' => $row['bank_name'] ?? null,
            'account_number' => $row['account_number'] ?? null,
            'account_name' => $row['account_name'] ?? null,
            'bank_code' => $row['bank_code'] ?? null,
            // 'pfa_pin' => $row['pfa_pin'] ?? null,
            'pfa_name' => $row['pfa_name'] ?? null,
            // 'pfa_plan_id' => $row['pfa_plan_id'] ?? null,
            'tax_id' => $row['tax_id'] ?? null,
            'nhf_number' => $row['nhf_number'] ?? null,
        ]);

        return new Employee([
            'user_id' => $user->id,
            'name' => trim(($row['first_name'] ?? '') . ' ' . ($row['last_name'] ?? '')),
            'dob' => $row['dob'] ?? null,
            'gender' => $row['gender'] ?? null,
            'phone' => $row['work_phone'] ?? null,
            'address' => $row['address'] ?? null,
            'email' => $row['email'],
            'password' => Hash::make('password'),
            'employee_id' => $user->id,
            'branch_id' => $branch ? $branch->id : 0,
            'department_id' => 0,
            'designation_id' => 0,
            'account_holder_name' => $row['account_name'] ?? null,
            'account_number' => $row['account_number'] ?? null,
            'bank_name' => $row['bank_name'] ?? null,
            'tax_payer_id' => $row['tax_id'] ?? null,
            'salary' => $row['gross_salary_amount'] ?? 0,
            'is_active' => 1,
        ]);
    }