class PayrollService:
    """
    Handles Salary Calculations and Payslip Generation.
    """

    @staticmethod
    def calculate_tax_pph21(annual_taxable_income, threshold=60000000) -> float:
        """
        Calculates PPh21 based on simple progressive rate.
        Note: Real world logic is complex (PTKP, Jabatan, etc).
        This is a simplified version for the prototype.

        Args:
            annual_taxable_income (float): Total yearly income.
            threshold (float): Non-taxable income threshold (PTKP).

        Returns:
            float: Annual Tax Amount.
        """
        if annual_taxable_income <= threshold:
            return 0.0

        taxable = annual_taxable_income - threshold
        tax = 0.0

        # Simplified Tier 1: 5% up to 60jt (after PTKP)
        # Simplified Tier 2: 15% above
        if taxable <= 60000000:
            tax = taxable * 0.05
        else:
            tax = (60000000 * 0.05) + ((taxable - 60000000) * 0.15)

        return tax

    @staticmethod
    def generate_payslip(employee, start_date, end_date):
        """
        Generates a draft payslip.
        """
        # TODO: Retrieve settings for PTKP/Threshold
        # settings = Settings.get('tax_threshold') ...

        # 1. Base Salary
        monthly_base = employee.base_salary

        # 2. Add Fixed Allowances (Placeholder)
        allowances = 0.0

        # 3. Calculate BPJS
        from app.core.services.settings import SettingsService
        bpjs_rate = float(SettingsService.get('company_bpjs_rate', '1.0')) / 100.0
        bpjs_deduction = monthly_base * bpjs_rate

        # 4. Calculate PPh21 (Annualized)
        annual_income = monthly_base * 12
        annual_tax = PayrollService.calculate_tax_pph21(annual_income)
        monthly_tax = annual_tax / 12

        total_deductions = bpjs_deduction + monthly_tax
        net_salary = monthly_base + allowances - total_deductions

        return {
            'base_salary': monthly_base,
            'allowances': allowances,
            'deductions': total_deductions,
            'tax': monthly_tax,
            'net_salary': net_salary
        }
