<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Employee;
use App\Models\Attendance;
use App\Models\User;
use App\Models\Role;
use App\Models\Salary;
use App\Models\Asset;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;

class TestDataSeeder extends Seeder
{
    public function run(): void
    {
        $adminRole = Role::where('name', 'Admin')->first();
        $employeeRole = Role::where('name', 'Staff')->first();

        // Sample Data
        $names = [
            'Arun Soman', 'Christeena', 'Divya', 'Emil', 'Mariya', 
            'Paul', 'Sandra J', 'Riyan Vinod', 'Safvam', 'Athul', 
            'Vijay', 'Akhilesh Jayan', 'Sibin'
        ];

        $cto = Employee::where('designation', 'CTO')->first();
        if (!$cto) {
            $cto = Employee::create([
                'name' => 'Jane Smith',
                'email' => 'jane@example.com',
                'designation' => 'CTO',
                'base_salary' => 80000,
                'joined_date' => '2021-02-15'
            ]);
        }

        foreach ($names as $idx => $name) {
            $email = strtolower(str_replace(' ', '.', $name)) . '@hrms.com';
            
            if (Employee::where('email', $email)->exists()) {
                $employee = Employee::where('email', $email)->first();
            } else {
                $employee = Employee::create([
                    'name' => $name,
                    'email' => $email,
                    'designation' => 'Team Lead',
                    'manager_id' => $cto->id,
                    'base_salary' => rand(40000, 70000),
                    'joined_date' => Carbon::now()->subMonths(rand(1, 48))->format('Y-m-d')
                ]);

                User::create([
                    'name' => $employee->name,
                    'email' => $employee->email,
                    'password' => Hash::make('password'),
                    'role_id' => $employeeRole->id,
                    'employee_id' => $employee->id,
                ]);
            }

            // Attendance - Last 30 days
            for ($i = 0; $i < 30; $i++) {
                $date = Carbon::now()->subDays($i);
                if ($date->isWeekend()) continue;

                $dayStr = $date->format('Y-m-d');
                if (Attendance::where('employee_id', $employee->id)->where('date', $dayStr)->exists()) continue;

                Attendance::create([
                    'employee_id' => $employee->id,
                    'date' => $dayStr,
                    'punch_in' => $date->copy()->setTime(9, rand(0, 30)),
                    'punch_out' => $date->copy()->setTime(18, rand(0, 45)),
                ]);
            }

            // Salaries - Current and past month
            foreach ([0, 1] as $mOffset) {
                $date = Carbon::now()->subMonths($mOffset);
                if (Salary::where('employee_id', $employee->id)->where('month', $date->month)->where('year', $date->year)->exists()) continue;

                Salary::create([
                    'employee_id' => $employee->id,
                    'month' => $date->month,
                    'year' => $date->year,
                    'base_salary' => $employee->base_salary,
                    'bonus' => rand(0, 1) ? rand(1000, 5000) : 0,
                    'deductions' => rand(500, 2000),
                    'net_salary' => $employee->base_salary, // Net salary would normally be calculated
                    'status' => $mOffset == 0 ? 'pending' : 'paid',
                    'payment_date' => $mOffset == 1 ? Carbon::now()->subMonth()->startOfMonth()->addDays(4) : null,
                ]);
            }
        }

        // Assets
        $assetNames = ['MacBook Pro 14', 'Dell UltraSharp 27', 'Logitech MX Master 3', 'iPhone 15 Pro', 'ThinkPad X1 Carbon'];
        foreach ($assetNames as $idx => $aName) {
            $aId = 'AST-' . (1000 + $idx);
            if (Asset::where('asset_id', $aId)->exists()) continue;

            Asset::create([
                'asset_id' => $aId,
                'name' => $aName,
                'category' => 'Hardware',
                'serial_number' => 'SN-' . strtoupper(bin2hex(random_bytes(4))),
                'purchase_date' => Carbon::now()->subYear(),
                'status' => 'Assigned',
                'assigned_to' => Employee::all()->random()->id,
                'assigned_date' => Carbon::now()->subMonths(3),
            ]);
        }
    }
}
