package models

import "odoo-go/pkg/orm"

// initResourceCalendar registers resource.calendar — working schedules.
// Mirrors: odoo/addons/resource/models/resource.py
func initResourceCalendar() {
	m := orm.NewModel("resource.calendar", orm.ModelOpts{
		Description: "Resource Working Time",
		Order:       "name",
	})

	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Name", Required: true}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
		orm.Float("hours_per_week", orm.FieldOpts{String: "Hours per Week"}),
		orm.Char("tz", orm.FieldOpts{String: "Timezone", Default: "Europe/Berlin"}),
		orm.Boolean("flexible_hours", orm.FieldOpts{String: "Flexible Hours"}),
		orm.One2many("attendance_ids", "resource.calendar.attendance", "calendar_id", orm.FieldOpts{String: "Attendances"}),
	)

	// resource.calendar.attendance — work time slots
	orm.NewModel("resource.calendar.attendance", orm.ModelOpts{
		Description: "Work Detail",
		Order:       "dayofweek, hour_from",
	}).AddFields(
		orm.Char("name", orm.FieldOpts{String: "Name", Required: true}),
		orm.Selection("dayofweek", []orm.SelectionItem{
			{Value: "0", Label: "Monday"}, {Value: "1", Label: "Tuesday"},
			{Value: "2", Label: "Wednesday"}, {Value: "3", Label: "Thursday"},
			{Value: "4", Label: "Friday"}, {Value: "5", Label: "Saturday"},
			{Value: "6", Label: "Sunday"},
		}, orm.FieldOpts{String: "Day of Week", Required: true}),
		orm.Float("hour_from", orm.FieldOpts{String: "Work from", Required: true}),
		orm.Float("hour_to", orm.FieldOpts{String: "Work to", Required: true}),
		orm.Many2one("calendar_id", "resource.calendar", orm.FieldOpts{
			String: "Calendar", Required: true, OnDelete: orm.OnDeleteCascade,
		}),
	)
}

// initHREmployee registers the hr.employee model.
// Mirrors: odoo/addons/hr/models/hr_employee.py
func initHREmployee() {
	m := orm.NewModel("hr.employee", orm.ModelOpts{
		Description: "Employee",
		Order:       "name",
	})

	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Employee Name", Required: true, Index: true}),
		orm.Many2one("user_id", "res.users", orm.FieldOpts{String: "Related User"}),
		orm.Many2one("department_id", "hr.department", orm.FieldOpts{String: "Department", Index: true}),
		orm.Many2one("job_id", "hr.job", orm.FieldOpts{String: "Job Position"}),
		orm.Char("job_title", orm.FieldOpts{String: "Job Title"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{
			String: "Company", Required: true, Index: true,
		}),
		orm.Many2one("parent_id", "hr.employee", orm.FieldOpts{String: "Manager", Index: true}),
		orm.Many2one("address_id", "res.partner", orm.FieldOpts{String: "Work Address"}),
		orm.Many2one("address_home_id", "res.partner", orm.FieldOpts{
			String: "Private Address", Groups: "hr.group_hr_user",
		}),
		orm.Char("identification_id", orm.FieldOpts{String: "Identification No", Groups: "hr.group_hr_user"}),
		orm.Char("work_email", orm.FieldOpts{String: "Work Email"}),
		orm.Char("work_phone", orm.FieldOpts{String: "Work Phone"}),
		orm.Char("mobile_phone", orm.FieldOpts{String: "Work Mobile"}),
		orm.Many2one("coach_id", "hr.employee", orm.FieldOpts{String: "Coach"}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
		orm.Many2one("resource_calendar_id", "resource.calendar", orm.FieldOpts{String: "Working Schedule"}),
		orm.Selection("gender", []orm.SelectionItem{
			{Value: "male", Label: "Male"},
			{Value: "female", Label: "Female"},
			{Value: "other", Label: "Other"},
		}, orm.FieldOpts{String: "Gender"}),
		orm.Date("birthday", orm.FieldOpts{String: "Date of Birth", Groups: "hr.group_hr_user"}),
		orm.Selection("marital", []orm.SelectionItem{
			{Value: "single", Label: "Single"},
			{Value: "married", Label: "Married"},
			{Value: "cohabitant", Label: "Legal Cohabitant"},
			{Value: "widower", Label: "Widower"},
			{Value: "divorced", Label: "Divorced"},
		}, orm.FieldOpts{String: "Marital Status", Default: "single"}),
		orm.Char("emergency_contact", orm.FieldOpts{String: "Emergency Contact"}),
		orm.Char("emergency_phone", orm.FieldOpts{String: "Emergency Phone"}),
		orm.Selection("certificate", []orm.SelectionItem{
			{Value: "graduate", Label: "Graduate"},
			{Value: "bachelor", Label: "Bachelor"},
			{Value: "master", Label: "Master"},
			{Value: "doctor", Label: "Doctor"},
			{Value: "other", Label: "Other"},
		}, orm.FieldOpts{String: "Certificate Level"}),
		orm.Char("study_field", orm.FieldOpts{String: "Field of Study"}),
		orm.Char("visa_no", orm.FieldOpts{String: "Visa No", Groups: "hr.group_hr_user"}),
		orm.Char("permit_no", orm.FieldOpts{String: "Work Permit No", Groups: "hr.group_hr_user"}),
		orm.Integer("km_home_work", orm.FieldOpts{String: "Home-Work Distance (km)"}),
		orm.Binary("image_1920", orm.FieldOpts{String: "Image"}),
	)

	// toggle_active: archive/unarchive employee
	m.RegisterMethod("toggle_active", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		env := rs.Env()
		for _, id := range rs.IDs() {
			env.Tx().Exec(env.Ctx(),
				`UPDATE hr_employee SET active = NOT active WHERE id = $1`, id)
		}
		return true, nil
	})
}

// initHrEmployeeExtensions adds skill, resume, attendance and leave fields
// to hr.employee after the related models have been registered.
func initHrEmployeeExtensions() {
	emp := orm.ExtendModel("hr.employee")
	emp.AddFields(
		orm.One2many("skill_ids", "hr.employee.skill", "employee_id", orm.FieldOpts{String: "Skills"}),
		orm.One2many("resume_line_ids", "hr.resume.line", "employee_id", orm.FieldOpts{String: "Resume"}),
		orm.One2many("attendance_ids", "hr.attendance", "employee_id", orm.FieldOpts{String: "Attendances"}),
		orm.Float("leaves_count", orm.FieldOpts{String: "Time Off", Compute: "_compute_leaves"}),
		orm.Selection("attendance_state", []orm.SelectionItem{
			{Value: "checked_out", Label: "Checked Out"},
			{Value: "checked_in", Label: "Checked In"},
		}, orm.FieldOpts{String: "Attendance", Compute: "_compute_attendance_state"}),
	)
}

// initHRDepartment registers the hr.department model.
// Mirrors: odoo/addons/hr/models/hr_department.py
func initHRDepartment() {
	m := orm.NewModel("hr.department", orm.ModelOpts{
		Description: "HR Department",
		Order:       "name",
	})

	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Department Name", Required: true, Translate: true}),
		orm.Char("complete_name", orm.FieldOpts{
			String:  "Complete Name",
			Compute: "_compute_complete_name",
			Store:   true,
		}),
		orm.Many2one("parent_id", "hr.department", orm.FieldOpts{String: "Parent Department", Index: true}),
		orm.Many2one("manager_id", "hr.employee", orm.FieldOpts{String: "Manager"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{
			String: "Company", Required: true, Index: true,
		}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
		orm.One2many("child_ids", "hr.department", "parent_id", orm.FieldOpts{String: "Child Departments"}),
		orm.One2many("member_ids", "hr.employee", "department_id", orm.FieldOpts{String: "Members"}),
	)
}

// initHRJob registers the hr.job model.
// Mirrors: odoo/addons/hr/models/hr_job.py
func initHRJob() {
	m := orm.NewModel("hr.job", orm.ModelOpts{
		Description: "Job Position",
		Order:       "name",
	})

	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Job Position", Required: true, Index: true, Translate: true}),
		orm.Many2one("department_id", "hr.department", orm.FieldOpts{String: "Department"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{
			String: "Company", Required: true, Index: true,
		}),
		orm.Integer("expected_employees", orm.FieldOpts{String: "Expected New Employees", Default: 1}),
		orm.Integer("no_of_recruitment", orm.FieldOpts{String: "Expected in Recruitment"}),
		orm.Integer("no_of_hired_employee", orm.FieldOpts{String: "Hired Employees"}),
		orm.Selection("state", []orm.SelectionItem{
			{Value: "recruit", Label: "Recruitment in Progress"},
			{Value: "open", Label: "Not Recruiting"},
		}, orm.FieldOpts{String: "Status", Required: true, Default: "recruit"}),
		orm.Text("description", orm.FieldOpts{String: "Job Description"}),
	)
}
