<?php

namespace Database\Seeders;

use App\Models\PayrollStructure;
use App\Models\SalaryComponent;
use Illuminate\Database\Seeder;

class PayrollStructureSeeder extends Seeder
{
    public function run(): void
    {
        $structure = PayrollStructure::updateOrCreate(
            ['name' => '6am_payroll'],
            ['description' => 'Standard payroll structure for 6am HRM']
        );

        $basic = SalaryComponent::where('name', 'Basic')->first();
        $medical = SalaryComponent::where('name', 'Medical')->first();
        $houseRent = SalaryComponent::where('name', 'House Rent')->first();
        $conveyance = SalaryComponent::where('name', 'Conveyance')->first();

        $components = [
            ['salary_component_id' => $basic->id, 'calculation_type' => 'percentage', 'value' => 50],
            ['salary_component_id' => $medical->id, 'calculation_type' => 'percentage', 'value' => 10],
            ['salary_component_id' => $houseRent->id, 'calculation_type' => 'percentage', 'value' => 30],
            ['salary_component_id' => $conveyance->id, 'calculation_type' => 'percentage', 'value' => 10],
        ];

        foreach ($components as $comp) {
            $structure->components()->updateOrCreate(
                ['salary_component_id' => $comp['salary_component_id']],
                $comp
            );
        }
    }
}
