package models

import "odoo-go/pkg/orm"

// initHrLeaveType registers the hr.leave.type model.
// Mirrors: odoo/addons/hr_holidays/models/hr_leave_type.py
func initHrLeaveType() {
	orm.NewModel("hr.leave.type", orm.ModelOpts{
		Description: "Time Off Type",
		Order:       "sequence, id",
	}).AddFields(
		orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}),
		orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 100}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
		orm.Selection("leave_validation_type", []orm.SelectionItem{
			{Value: "no_validation", Label: "No Validation"},
			{Value: "hr", Label: "By Time Off Officer"},
			{Value: "manager", Label: "By Employee's Approver"},
			{Value: "both", Label: "By Employee's Approver and Time Off Officer"},
		}, orm.FieldOpts{String: "Approval", Default: "hr"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
		orm.Integer("color", orm.FieldOpts{String: "Color"}),
		orm.Boolean("requires_allocation", orm.FieldOpts{String: "Requires Allocation", Default: true}),
		orm.Float("max_allowed", orm.FieldOpts{String: "Max Days Allowed"}),
	)
}

// initHrLeave registers the hr.leave model.
// Mirrors: odoo/addons/hr_holidays/models/hr_leave.py
func initHrLeave() {
	m := orm.NewModel("hr.leave", orm.ModelOpts{
		Description: "Time Off",
		Order:       "date_from desc",
	})
	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Description"}),
		orm.Many2one("employee_id", "hr.employee", orm.FieldOpts{String: "Employee", Required: true}),
		orm.Many2one("holiday_status_id", "hr.leave.type", orm.FieldOpts{String: "Time Off Type", Required: true}),
		orm.Many2one("department_id", "hr.department", orm.FieldOpts{String: "Department"}),
		orm.Many2one("manager_id", "hr.employee", orm.FieldOpts{String: "Manager"}),
		orm.Datetime("date_from", orm.FieldOpts{String: "Start Date", Required: true}),
		orm.Datetime("date_to", orm.FieldOpts{String: "End Date", Required: true}),
		orm.Float("number_of_days", orm.FieldOpts{String: "Duration (Days)"}),
		orm.Selection("state", []orm.SelectionItem{
			{Value: "draft", Label: "To Submit"},
			{Value: "confirm", Label: "To Approve"},
			{Value: "validate1", Label: "Second Approval"},
			{Value: "validate", Label: "Approved"},
			{Value: "refuse", Label: "Refused"},
		}, orm.FieldOpts{String: "Status", Default: "draft"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
		orm.Text("notes", orm.FieldOpts{String: "Reasons"}),
	)

	m.RegisterMethod("action_approve", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		env := rs.Env()
		for _, id := range rs.IDs() {
			env.Tx().Exec(env.Ctx(), `UPDATE hr_leave SET state = 'validate' WHERE id = $1 AND state IN ('confirm','validate1')`, id)
		}
		return true, nil
	})

	m.RegisterMethod("action_refuse", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		env := rs.Env()
		for _, id := range rs.IDs() {
			env.Tx().Exec(env.Ctx(), `UPDATE hr_leave SET state = 'refuse' WHERE id = $1`, id)
		}
		return true, nil
	})

	m.RegisterMethod("action_draft", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		env := rs.Env()
		for _, id := range rs.IDs() {
			env.Tx().Exec(env.Ctx(), `UPDATE hr_leave SET state = 'draft' WHERE id = $1`, id)
		}
		return true, nil
	})

	m.RegisterMethod("action_confirm", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		env := rs.Env()
		for _, id := range rs.IDs() {
			env.Tx().Exec(env.Ctx(), `UPDATE hr_leave SET state = 'confirm' WHERE id = $1 AND state = 'draft'`, id)
		}
		return true, nil
	})
}

// initHrLeaveAllocation registers the hr.leave.allocation model.
// Mirrors: odoo/addons/hr_holidays/models/hr_leave_allocation.py
func initHrLeaveAllocation() {
	m := orm.NewModel("hr.leave.allocation", orm.ModelOpts{
		Description: "Time Off Allocation",
		Order:       "create_date desc",
	})
	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Description"}),
		orm.Many2one("employee_id", "hr.employee", orm.FieldOpts{String: "Employee", Required: true}),
		orm.Many2one("holiday_status_id", "hr.leave.type", orm.FieldOpts{String: "Time Off Type", Required: true}),
		orm.Float("number_of_days", orm.FieldOpts{String: "Duration (Days)", Required: true}),
		orm.Selection("state", []orm.SelectionItem{
			{Value: "draft", Label: "To Submit"},
			{Value: "confirm", Label: "To Approve"},
			{Value: "validate", Label: "Approved"},
			{Value: "refuse", Label: "Refused"},
		}, orm.FieldOpts{String: "Status", Default: "draft"}),
		orm.Many2one("department_id", "hr.department", orm.FieldOpts{String: "Department"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
		orm.Selection("allocation_type", []orm.SelectionItem{
			{Value: "regular", Label: "Regular Allocation"},
			{Value: "accrual", Label: "Accrual Allocation"},
		}, orm.FieldOpts{String: "Allocation Type", Default: "regular"}),
	)

	m.RegisterMethod("action_approve", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		env := rs.Env()
		for _, id := range rs.IDs() {
			env.Tx().Exec(env.Ctx(), `UPDATE hr_leave_allocation SET state = 'validate' WHERE id = $1`, id)
		}
		return true, nil
	})
}
