<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Modules\HR\Core\Models\Designation;
use App\Modules\HR\Payroll\Models\Addition;
use App\Modules\HR\Payroll\Models\Deduction;

class DesignationPayrollSeeder extends Seeder
{
    public function run(): void
    {
        $additionIds  = Addition::pluck('id');
        $deductionIds = Deduction::pluck('id');

        if ($additionIds->isEmpty() && $deductionIds->isEmpty()) {
            $this->command->warn('No additions or deductions found. Skipping.');
            return;
        }

        $designations = Designation::all();

        if ($designations->isEmpty()) {
            $this->command->warn('No designations found. Skipping.');
            return;
        }

        $this->command->info('Linking additions and deductions to all designations...');

        foreach ($designations as $designation) {
            if ($additionIds->isNotEmpty()) {
                $designation->additions()->syncWithoutDetaching($additionIds->toArray());
            }
            if ($deductionIds->isNotEmpty()) {
                $designation->deductions()->syncWithoutDetaching($deductionIds->toArray());
            }

            $this->command->info(
                "  {$designation->name}: {$additionIds->count()} additions, {$deductionIds->count()} deductions"
            );
        }

        $this->command->info('');
        $this->command->info('Done.');
        $this->command->info('  Designations : ' . $designations->count());
        $this->command->info('  Additions    : ' . $additionIds->count());
        $this->command->info('  Deductions   : ' . $deductionIds->count());
    }
}
