package models

import "odoo-go/pkg/orm"

// initResPartner registers the res.partner model.
// Mirrors: odoo/addons/base/models/res_partner.py class Partner(models.Model)
//
// res.partner is the central contact model in Odoo. It stores:
// - Companies and individuals
// - Customers, vendors, employees (via type)
// - Addresses (street, city, zip, country)
// - Communication (email, phone, website)
func initResPartner() {
	m := orm.NewModel("res.partner", orm.ModelOpts{
		Description: "Contact",
		Order:       "name, id",
		RecName:     "name",
	})

	// -- Identity --
	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Index: true}),
		orm.Char("ref", orm.FieldOpts{String: "Reference", Index: true}),
		orm.Selection("type", []orm.SelectionItem{
			{Value: "contact", Label: "Contact"},
			{Value: "invoice", Label: "Invoice Address"},
			{Value: "delivery", Label: "Delivery Address"},
			{Value: "other", Label: "Other Address"},
			{Value: "private", Label: "Private Address"},
		}, orm.FieldOpts{String: "Address Type", Default: "contact"}),
		orm.Boolean("is_company", orm.FieldOpts{String: "Is a Company", Default: false}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
		orm.Char("lang", orm.FieldOpts{String: "Language", Default: "en_US"}),
		orm.Char("tz", orm.FieldOpts{String: "Timezone"}),
	)

	// -- Address --
	m.AddFields(
		orm.Char("street", orm.FieldOpts{String: "Street"}),
		orm.Char("street2", orm.FieldOpts{String: "Street2"}),
		orm.Char("zip", orm.FieldOpts{String: "Zip"}),
		orm.Char("city", orm.FieldOpts{String: "City"}),
		orm.Many2one("state_id", "res.country.state", orm.FieldOpts{String: "State"}),
		orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
	)

	// -- Communication --
	m.AddFields(
		orm.Char("email", orm.FieldOpts{String: "Email"}),
		orm.Char("phone", orm.FieldOpts{String: "Phone"}),
		orm.Char("mobile", orm.FieldOpts{String: "Mobile"}),
		orm.Char("website", orm.FieldOpts{String: "Website"}),
		orm.Char("vat", orm.FieldOpts{String: "Tax ID", Index: true}),
	)

	// -- Company --
	m.AddFields(
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company", Index: true}),
		orm.Char("company_name", orm.FieldOpts{String: "Company Name"}),
		orm.Char("company_registry", orm.FieldOpts{String: "Company Registry"}),
	)

	// -- Relationships --
	m.AddFields(
		orm.Many2one("parent_id", "res.partner", orm.FieldOpts{String: "Related Company", Index: true}),
		orm.One2many("child_ids", "res.partner", "parent_id", orm.FieldOpts{String: "Contact & Addresses"}),
		orm.Many2many("category_ids", "res.partner.category", orm.FieldOpts{String: "Tags"}),
		orm.Many2one("user_id", "res.users", orm.FieldOpts{String: "Salesperson"}),
		orm.Many2one("title", "res.partner.title", orm.FieldOpts{String: "Title"}),
	)

	// -- Commercial --
	m.AddFields(
		orm.Many2one("commercial_partner_id", "res.partner", orm.FieldOpts{
			String: "Commercial Entity", Compute: "_compute_commercial_partner", Store: true,
		}),
		orm.Selection("customer_rank", []orm.SelectionItem{
			{Value: "0", Label: "None"},
			{Value: "1", Label: "Customer"},
		}, orm.FieldOpts{String: "Customer Rank", Default: "0"}),
		orm.Selection("supplier_rank", []orm.SelectionItem{
			{Value: "0", Label: "None"},
			{Value: "1", Label: "Vendor"},
		}, orm.FieldOpts{String: "Vendor Rank", Default: "0"}),
	)

	// -- Banking --
	m.AddFields(
		orm.One2many("bank_ids", "res.partner.bank", "partner_id", orm.FieldOpts{String: "Bank Accounts"}),
	)

	// -- Notes --
	m.AddFields(
		orm.Text("comment", orm.FieldOpts{String: "Notes"}),
		orm.Binary("image_1920", orm.FieldOpts{String: "Image"}),
	)

	// --- Supporting models ---

	// res.partner.category — Contact tags
	// Mirrors: odoo/addons/base/models/res_partner.py class PartnerCategory
	cat := orm.NewModel("res.partner.category", orm.ModelOpts{
		Description: "Contact Tag",
		Order:       "name",
	})
	cat.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Tag Name", Required: true}),
		orm.Integer("color", orm.FieldOpts{String: "Color Index"}),
		orm.Many2one("parent_id", "res.partner.category", orm.FieldOpts{String: "Parent Category"}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
	)

	// res.partner.title — Contact titles (Mr., Mrs., etc.)
	// Mirrors: odoo/addons/base/models/res_partner.py class PartnerTitle
	title := orm.NewModel("res.partner.title", orm.ModelOpts{
		Description: "Partner Title",
		Order:       "name",
	})
	title.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Title", Required: true, Translate: true}),
		orm.Char("shortcut", orm.FieldOpts{String: "Abbreviation", Translate: true}),
	)

	// res.partner.bank — Bank accounts
	// Mirrors: odoo/addons/base/models/res_bank.py class ResPartnerBank
	bank := orm.NewModel("res.partner.bank", orm.ModelOpts{
		Description: "Bank Accounts",
		Order:       "id",
		RecName:     "acc_number",
	})
	bank.AddFields(
		orm.Char("acc_number", orm.FieldOpts{String: "Account Number", Required: true}),
		orm.Char("sanitized_acc_number", orm.FieldOpts{String: "Sanitized Account Number", Compute: "_compute_sanitized"}),
		orm.Many2one("partner_id", "res.partner", orm.FieldOpts{String: "Account Holder", Required: true, OnDelete: orm.OnDeleteCascade}),
		orm.Many2one("bank_id", "res.bank", orm.FieldOpts{String: "Bank"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
		orm.Char("acc_holder_name", orm.FieldOpts{String: "Account Holder Name"}),
	)

	// res.bank — Bank directory
	resBank := orm.NewModel("res.bank", orm.ModelOpts{
		Description: "Bank",
		Order:       "name",
	})
	resBank.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Name", Required: true}),
		orm.Char("bic", orm.FieldOpts{String: "Bank Identifier Code", Index: true}),
		orm.Char("street", orm.FieldOpts{String: "Street"}),
		orm.Char("street2", orm.FieldOpts{String: "Street2"}),
		orm.Char("zip", orm.FieldOpts{String: "Zip"}),
		orm.Char("city", orm.FieldOpts{String: "City"}),
		orm.Many2one("country", "res.country", orm.FieldOpts{String: "Country"}),
		orm.Char("email", orm.FieldOpts{String: "Email"}),
		orm.Char("phone", orm.FieldOpts{String: "Phone"}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
	)
}
