package models

import "odoo-go/pkg/orm"

// initResConfigSettings registers the res.config.settings transient model.
// Mirrors: odoo/addons/base/models/res_config.py class ResConfigSettings(TransientModel)
//
// This wizard provides the Settings form. Each "save" creates a new transient
// record, applies the values, then the record is eventually cleaned up.
func initResConfigSettings() {
	m := orm.NewModel("res.config.settings", orm.ModelOpts{
		Description: "Config Settings",
		Type:        orm.ModelTransient,
	})

	// -- General settings --
	m.AddFields(
		orm.Many2one("company_id", "res.company", orm.FieldOpts{
			String: "Company", Index: true,
		}),
		orm.Boolean("user_default_rights", orm.FieldOpts{String: "Default Access Rights"}),
		orm.Boolean("external_email_server_default", orm.FieldOpts{String: "External Email Servers"}),
		orm.Boolean("module_base_import", orm.FieldOpts{String: "Allow Import"}),
		orm.Boolean("module_google_calendar", orm.FieldOpts{String: "Google Calendar"}),
		orm.Boolean("group_multi_company", orm.FieldOpts{String: "Multi Companies"}),
		orm.Boolean("show_effect", orm.FieldOpts{String: "Show Effect", Default: true}),
	)

	// -- Company info fields (mirrors res.company, for display in settings) --
	// Mirrors: odoo/addons/base/models/res_config_settings.py company-related fields
	m.AddFields(
		orm.Char("company_name", orm.FieldOpts{String: "Company Name", Related: "company_id.name"}),
		orm.Many2one("company_currency_id", "res.currency", orm.FieldOpts{
			String: "Currency", Related: "company_id.currency_id",
		}),
		orm.Many2one("company_country_id", "res.country", orm.FieldOpts{
			String: "Country", Related: "company_id.country_id",
		}),
		orm.Char("company_street", orm.FieldOpts{String: "Street", Related: "company_id.street"}),
		orm.Char("company_street2", orm.FieldOpts{String: "Street2", Related: "company_id.street2"}),
		orm.Char("company_zip", orm.FieldOpts{String: "Zip", Related: "company_id.zip"}),
		orm.Char("company_city", orm.FieldOpts{String: "City", Related: "company_id.city"}),
		orm.Char("company_phone", orm.FieldOpts{String: "Phone", Related: "company_id.phone"}),
		orm.Char("company_email", orm.FieldOpts{String: "Email", Related: "company_id.email"}),
		orm.Char("company_website", orm.FieldOpts{String: "Website", Related: "company_id.website"}),
		orm.Char("company_vat", orm.FieldOpts{String: "Tax ID", Related: "company_id.vat"}),
		orm.Char("company_registry", orm.FieldOpts{String: "Company Registry", Related: "company_id.company_registry"}),
	)

	// -- Accounting settings --
	m.AddFields(
		orm.Char("chart_template", orm.FieldOpts{String: "Chart of Accounts"}),
		orm.Selection("tax_calculation_rounding_method", []orm.SelectionItem{
			{Value: "round_per_line", Label: "Round per Line"},
			{Value: "round_globally", Label: "Round Globally"},
		}, orm.FieldOpts{String: "Tax Calculation Rounding Method", Default: "round_per_line"}),
	)

	// execute: called by the Settings form to apply configuration.
	// Mirrors: odoo/addons/base/models/res_config.py ResConfigSettings.execute()
	m.RegisterMethod("execute", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		// In Python Odoo this writes Related fields back to res.company.
		// For now we just return true; the Related fields are read-only display.
		return true, nil
	})

	// DefaultGet: pre-fill from current company.
	// Mirrors: odoo/addons/base/models/res_config.py ResConfigSettings.default_get()
	m.DefaultGet = func(env *orm.Environment, fields []string) orm.Values {
		vals := orm.Values{
			"company_id": env.CompanyID(),
			"show_effect": true,
		}
		return vals
	}
}
