package models

import (
	"fmt"

	"odoo-go/pkg/orm"
	"odoo-go/pkg/tools"
)

// initResUsers registers the res.users model.
// Mirrors: odoo/addons/base/models/res_users.py class Users
//
// In Odoo, res.users inherits from res.partner via _inherits.
// Every user has a linked partner record for contact info.
func initResUsers() {
	m := orm.NewModel("res.users", orm.ModelOpts{
		Description: "Users",
		Order:       "login",
		Inherits:    map[string]string{"res.partner": "partner_id"},
	})

	// -- Authentication --
	m.AddFields(
		orm.Char("login", orm.FieldOpts{String: "Login", Required: true, Index: true}),
		orm.Char("password", orm.FieldOpts{String: "Password"}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
	)

	// -- Partner link (Odoo: _inherits = {'res.partner': 'partner_id'}) --
	m.AddFields(
		orm.Many2one("partner_id", "res.partner", orm.FieldOpts{
			String: "Related Partner", Required: true, OnDelete: orm.OnDeleteRestrict,
		}),
		orm.Char("name", orm.FieldOpts{String: "Name", Related: "partner_id.name"}),
		orm.Char("email", orm.FieldOpts{String: "Email", Related: "partner_id.email"}),
	)

	// -- Company --
	m.AddFields(
		orm.Many2one("company_id", "res.company", orm.FieldOpts{
			String: "Company", Required: true, Index: true,
		}),
		orm.Many2many("company_ids", "res.company", orm.FieldOpts{String: "Allowed Companies"}),
	)

	// -- Groups / Permissions --
	m.AddFields(
		orm.Many2many("groups_id", "res.groups", orm.FieldOpts{String: "Groups"}),
	)

	// -- Preferences --
	m.AddFields(
		orm.Char("lang", orm.FieldOpts{String: "Language", Default: "en_US"}),
		orm.Char("tz", orm.FieldOpts{String: "Timezone", Default: "UTC"}),
		orm.Selection("notification_type", []orm.SelectionItem{
			{Value: "email", Label: "Handle by Emails"},
			{Value: "inbox", Label: "Handle in Odoo"},
		}, orm.FieldOpts{String: "Notification", Default: "email"}),
		orm.Binary("image_1920", orm.FieldOpts{String: "Avatar"}),
		orm.Char("signature", orm.FieldOpts{String: "Email Signature"}),
	)

	// -- Status --
	m.AddFields(
		orm.Boolean("share", orm.FieldOpts{
			String: "Share User", Compute: "_compute_share", Store: true,
			Help: "External user with limited access (portal/public)",
		}),
	)

	// -- Methods --

	// action_get returns the "Change My Preferences" action for the current user.
	// Mirrors: odoo/addons/base/models/res_users.py Users.action_get()
	m.RegisterMethod("action_get", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		return map[string]interface{}{
			"type":      "ir.actions.act_window",
			"name":      "Change My Preferences",
			"res_model": "res.users",
			"view_mode": "form",
			"views":     [][]interface{}{{false, "form"}},
			"target":    "new",
			"res_id":    rs.Env().UID(),
			"context":   map[string]interface{}{},
		}, nil
	})

	// change_password: verifies old password and sets a new one.
	// Mirrors: odoo/addons/base/models/res_users.py Users.change_password()
	m.RegisterMethod("change_password", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		if len(args) < 2 {
			return false, fmt.Errorf("change_password requires old_password and new_password")
		}
		oldPw, _ := args[0].(string)
		newPw, _ := args[1].(string)

		env := rs.Env()
		uid := env.UID()

		// Verify old password
		var hashedPw string
		err := env.Tx().QueryRow(env.Ctx(),
			`SELECT password FROM res_users WHERE id = $1`, uid).Scan(&hashedPw)
		if err != nil {
			return false, fmt.Errorf("user not found")
		}
		if !tools.CheckPassword(hashedPw, oldPw) {
			return false, fmt.Errorf("incorrect old password")
		}

		// Hash and set new password
		newHash, err := tools.HashPassword(newPw)
		if err != nil {
			return false, err
		}
		_, err = env.Tx().Exec(env.Ctx(),
			`UPDATE res_users SET password = $1 WHERE id = $2`, newHash, uid)
		if err != nil {
			return false, err
		}
		return true, nil
	})

	// preference_save: called when saving user preferences.
	// Mirrors: odoo/addons/base/models/res_users.py Users.preference_save()
	m.RegisterMethod("preference_save", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		// Preferences are saved via normal write; just return true.
		return true, nil
	})

	// preference_change_password: alias for change_password from preferences dialog.
	// Mirrors: odoo/addons/base/models/res_users.py Users.preference_change_password()
	m.RegisterMethod("preference_change_password", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		return rs.Env().Model("res.users").Browse(rs.Env().UID()).Read([]string{"id"})
	})
}

// initResGroups registers the res.groups model.
// Mirrors: odoo/addons/base/models/res_users.py class Groups
//
// Groups define permission sets. Users belong to groups.
// Groups can imply other groups (hierarchy).
func initResGroups() {
	m := orm.NewModel("res.groups", orm.ModelOpts{
		Description: "Access Groups",
		Order:       "name",
	})

	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}),
		orm.Text("comment", orm.FieldOpts{String: "Comment"}),
		orm.Many2one("category_id", "ir.module.category", orm.FieldOpts{String: "Application"}),
		orm.Char("color", orm.FieldOpts{String: "Color Index"}),
		orm.Char("full_name", orm.FieldOpts{String: "Group Name", Compute: "_compute_full_name"}),
		orm.Boolean("share", orm.FieldOpts{String: "Share Group", Default: false}),
	)

	// -- Relationships --
	m.AddFields(
		orm.Many2many("users", "res.users", orm.FieldOpts{String: "Users"}),
		orm.Many2many("implied_ids", "res.groups", orm.FieldOpts{
			String: "Inherits",
			Help:   "Users of this group automatically inherit those groups",
		}),
	)

	// -- Access Control --
	m.AddFields(
		orm.One2many("model_access", "ir.model.access", "group_id", orm.FieldOpts{String: "Access Controls"}),
		orm.One2many("rule_groups", "ir.rule", "group_id", orm.FieldOpts{String: "Rules"}),
		orm.Many2many("menu_access", "ir.ui.menu", orm.FieldOpts{String: "Access Menu"}),
	)
}
