import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  OneToMany,
} from "typeorm"
import { BaseEntity } from "../../common/entities/base.entity"
import { Company } from "../../company/entities/company.entity"
import { Employee } from "../../employees/entities/employee.entity"
import { PayrollCycle } from "./payroll-cycle.entity"
import { PayrollEntryComponent } from "./payroll-entry-component.entity"

@Entity("payroll_entries")
export class PayrollEntry extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ type: "int", nullable: false })
  company_id: number

  @Column({ type: "int", nullable: false })
  payroll_cycle_id: number

  @Column({ type: "int", nullable: false })
  employee_id: number

  // Attendance data
  @Column({ type: "int", nullable: false })
  working_days: number

  @Column({ type: "int", nullable: false })
  present_days: number

  @Column({
    type: "decimal",
    precision: 5,
    scale: 2,
    nullable: false,
    default: 0,
  })
  paid_leaves: number

  @Column({
    type: "decimal",
    precision: 5,
    scale: 2,
    nullable: false,
    default: 0,
  })
  unpaid_leaves: number

  // Summary totals
  @Column({ type: "decimal", precision: 12, scale: 2, nullable: false })
  total_earnings: number

  @Column({ type: "decimal", precision: 12, scale: 2, nullable: false })
  total_deductions: number

  @Column({ type: "decimal", precision: 12, scale: 2, nullable: false })
  net_payable: number

  // Relations
  @ManyToOne(() => Company)
  @JoinColumn({ name: "company_id" })
  company: Company

  @ManyToOne(() => PayrollCycle)
  @JoinColumn({ name: "payroll_cycle_id" })
  payroll_cycle: PayrollCycle

  @ManyToOne(() => Employee)
  @JoinColumn({ name: "employee_id" })
  employee: Employee

  @OneToMany(
    () => PayrollEntryComponent,
    (component) => component.payroll_entry,
  )
  components: PayrollEntryComponent[]
}
