package models

import "odoo-go/pkg/orm"

// initAccountAccount registers the chart of accounts.
// Mirrors: odoo/addons/account/models/account_account.py

func initAccountAccount() {
	// account.account — Chart of Accounts
	m := orm.NewModel("account.account", orm.ModelOpts{
		Description: "Account",
		Order:       "code, company_id",
		RecName:     "name",
	})

	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Account Name", Required: true, Index: true, Translate: true}),
		orm.Char("code", orm.FieldOpts{String: "Code", Required: true, Size: 64, Index: true}),
		orm.Selection("account_type", []orm.SelectionItem{
			// Balance Sheet
			{Value: "asset_receivable", Label: "Receivable"},
			{Value: "asset_cash", Label: "Bank and Cash"},
			{Value: "asset_current", Label: "Current Assets"},
			{Value: "asset_non_current", Label: "Non-current Assets"},
			{Value: "asset_prepayments", Label: "Prepayments"},
			{Value: "asset_fixed", Label: "Fixed Assets"},
			{Value: "liability_payable", Label: "Payable"},
			{Value: "liability_credit_card", Label: "Credit Card"},
			{Value: "liability_current", Label: "Current Liabilities"},
			{Value: "liability_non_current", Label: "Non-current Liabilities"},
			{Value: "equity", Label: "Equity"},
			{Value: "equity_unaffected", Label: "Current Year Earnings"},
			// P&L
			{Value: "income", Label: "Income"},
			{Value: "income_other", Label: "Other Income"},
			{Value: "expense", Label: "Expenses"},
			{Value: "expense_depreciation", Label: "Depreciation"},
			{Value: "expense_direct_cost", Label: "Cost of Revenue"},
			// Special
			{Value: "off_balance", Label: "Off-Balance Sheet"},
		}, orm.FieldOpts{String: "Type", Required: true}),
		orm.Boolean("reconcile", orm.FieldOpts{String: "Allow Reconciliation"}),
		orm.Boolean("non_trade", orm.FieldOpts{String: "Non-Trade Receivable/Payable"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{
			String: "Company", Required: true, Index: true,
		}),
		orm.Many2one("currency_id", "res.currency", orm.FieldOpts{String: "Account Currency"}),
		orm.Many2many("tax_ids", "account.tax", orm.FieldOpts{String: "Default Taxes"}),
		orm.Many2many("tag_ids", "account.account.tag", orm.FieldOpts{String: "Tags"}),
		orm.Text("note", orm.FieldOpts{String: "Internal Notes"}),
		orm.Boolean("deprecated", orm.FieldOpts{String: "Deprecated"}),
		orm.Boolean("include_initial_balance", orm.FieldOpts{String: "Include Initial Balance"}),
	)

	// account.account.tag — Account tags for reporting
	orm.NewModel("account.account.tag", orm.ModelOpts{
		Description: "Account Tag",
		Order:       "name",
	}).AddFields(
		orm.Char("name", orm.FieldOpts{String: "Tag Name", Required: true, Translate: true}),
		orm.Selection("applicability", []orm.SelectionItem{
			{Value: "accounts", Label: "Accounts"},
			{Value: "taxes", Label: "Taxes"},
			{Value: "products", Label: "Products"},
		}, orm.FieldOpts{String: "Applicability", Default: "accounts", Required: true}),
		orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
		orm.Char("color", orm.FieldOpts{String: "Color Index"}),
	)

	// account.group — Account groups for hierarchy
	orm.NewModel("account.group", orm.ModelOpts{
		Description: "Account Group",
		Order:       "code_prefix_start",
	}).AddFields(
		orm.Char("name", orm.FieldOpts{String: "Account Group", Required: true, Translate: true}),
		orm.Char("code_prefix_start", orm.FieldOpts{String: "Code Prefix Start"}),
		orm.Char("code_prefix_end", orm.FieldOpts{String: "Code Prefix End"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company", Required: true}),
	)
}

// initAccountTax registers tax models.
// Mirrors: odoo/addons/account/models/account_tax.py
func initAccountTax() {
	m := orm.NewModel("account.tax", orm.ModelOpts{
		Description: "Tax",
		Order:       "sequence, id",
	})

	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Tax Name", Required: true, Translate: true}),
		orm.Selection("type_tax_use", []orm.SelectionItem{
			{Value: "sale", Label: "Sales"},
			{Value: "purchase", Label: "Purchases"},
			{Value: "none", Label: "None"},
		}, orm.FieldOpts{String: "Tax Type", Required: true, Default: "sale"}),
		orm.Selection("amount_type", []orm.SelectionItem{
			{Value: "group", Label: "Group of Taxes"},
			{Value: "fixed", Label: "Fixed"},
			{Value: "percent", Label: "Percentage of Price"},
			{Value: "division", Label: "Percentage of Price Tax Included"},
		}, orm.FieldOpts{String: "Tax Computation", Required: true, Default: "percent"}),
		orm.Float("amount", orm.FieldOpts{String: "Amount", Required: true}),
		orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 1}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company", Required: true}),
		orm.Boolean("price_include", orm.FieldOpts{String: "Included in Price"}),
		orm.Boolean("include_base_amount", orm.FieldOpts{String: "Affect Base of Subsequent Taxes"}),
		orm.Boolean("is_base_affected", orm.FieldOpts{String: "Base Affected by Previous Taxes", Default: true}),
		orm.Many2many("children_tax_ids", "account.tax", orm.FieldOpts{String: "Children Taxes"}),
		orm.Many2one("parent_tax_id", "account.tax", orm.FieldOpts{String: "Parent Tax Group"}),
		orm.Char("description", orm.FieldOpts{String: "Label on Invoices"}),
		orm.Many2one("tax_group_id", "account.tax.group", orm.FieldOpts{String: "Tax Group"}),
		orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
		// Repartition lines define how tax amounts are distributed to accounts
		orm.One2many("invoice_repartition_line_ids", "account.tax.repartition.line", "tax_id", orm.FieldOpts{String: "Invoice Repartition"}),
		orm.One2many("refund_repartition_line_ids", "account.tax.repartition.line", "tax_id", orm.FieldOpts{String: "Refund Repartition"}),
	)

	// account.tax.group — Tax group for reports
	orm.NewModel("account.tax.group", orm.ModelOpts{
		Description: "Tax Group",
		Order:       "sequence, name",
	}).AddFields(
		orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}),
		orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 10}),
		orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
	)

	// account.tax.repartition.line — How tax is split across accounts
	// Mirrors: odoo/addons/account/models/account_tax.py AccountTaxRepartitionLine
	orm.NewModel("account.tax.repartition.line", orm.ModelOpts{
		Description: "Tax Repartition Line",
		Order:       "sequence, id",
	}).AddFields(
		orm.Integer("sequence", orm.FieldOpts{String: "Sequence", Default: 1}),
		orm.Float("factor_percent", orm.FieldOpts{String: "Percentage", Default: 100, Required: true}),
		orm.Selection("repartition_type", []orm.SelectionItem{
			{Value: "base", Label: "Base"},
			{Value: "tax", Label: "Tax"},
		}, orm.FieldOpts{String: "Based On", Default: "tax", Required: true}),
		orm.Many2one("account_id", "account.account", orm.FieldOpts{String: "Account"}),
		orm.Many2one("tax_id", "account.tax", orm.FieldOpts{String: "Tax", OnDelete: orm.OnDeleteCascade}),
		orm.Many2many("tag_ids", "account.account.tag", orm.FieldOpts{String: "Tax Grids"}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company"}),
		orm.Selection("document_type", []orm.SelectionItem{
			{Value: "invoice", Label: "Invoice"},
			{Value: "refund", Label: "Refund"},
		}, orm.FieldOpts{String: "Document Type"}),
		orm.Boolean("use_in_tax_closing", orm.FieldOpts{String: "Tax Closing Entry", Default: true}),
	)

	// account.fiscal.position — Tax mapping per country/partner
	// Mirrors: odoo/addons/account/models/account_fiscal_position.py
	initAccountFiscalPosition()
}

func initAccountFiscalPosition() {
	m := orm.NewModel("account.fiscal.position", orm.ModelOpts{
		Description: "Fiscal Position",
		Order:       "sequence",
	})

	m.AddFields(
		orm.Char("name", orm.FieldOpts{String: "Fiscal Position", Required: true, Translate: true}),
		orm.Boolean("active", orm.FieldOpts{String: "Active", Default: true}),
		orm.Many2one("company_id", "res.company", orm.FieldOpts{String: "Company", Required: true}),
		orm.One2many("tax_ids", "account.fiscal.position.tax", "position_id", orm.FieldOpts{String: "Tax Mapping"}),
		orm.One2many("account_ids", "account.fiscal.position.account", "position_id", orm.FieldOpts{String: "Account Mapping"}),
		orm.Integer("sequence", orm.FieldOpts{String: "Sequence"}),
		orm.Boolean("auto_apply", orm.FieldOpts{String: "Detect Automatically"}),
		orm.Many2one("country_id", "res.country", orm.FieldOpts{String: "Country"}),
		orm.Many2one("country_group_id", "res.country.group", orm.FieldOpts{String: "Country Group"}),
		orm.Boolean("vat_required", orm.FieldOpts{String: "VAT required"}),
		orm.Text("note", orm.FieldOpts{String: "Notes", Translate: true}),
	)

	// Fiscal position tax mapping
	orm.NewModel("account.fiscal.position.tax", orm.ModelOpts{
		Description: "Tax Mapping",
	}).AddFields(
		orm.Many2one("position_id", "account.fiscal.position", orm.FieldOpts{String: "Fiscal Position", Required: true, OnDelete: orm.OnDeleteCascade}),
		orm.Many2one("tax_src_id", "account.tax", orm.FieldOpts{String: "Tax on Product", Required: true}),
		orm.Many2one("tax_dest_id", "account.tax", orm.FieldOpts{String: "Tax to Apply"}),
	)

	// Fiscal position account mapping
	orm.NewModel("account.fiscal.position.account", orm.ModelOpts{
		Description: "Account Mapping",
	}).AddFields(
		orm.Many2one("position_id", "account.fiscal.position", orm.FieldOpts{String: "Fiscal Position", Required: true, OnDelete: orm.OnDeleteCascade}),
		orm.Many2one("account_src_id", "account.account", orm.FieldOpts{String: "Account on Product", Required: true}),
		orm.Many2one("account_dest_id", "account.account", orm.FieldOpts{String: "Account to Use", Required: true}),
	)

	// res.country.group — for fiscal position country grouping
	orm.NewModel("res.country.group", orm.ModelOpts{
		Description: "Country Group",
		Order:       "name",
	}).AddFields(
		orm.Char("name", orm.FieldOpts{String: "Name", Required: true, Translate: true}),
		orm.Many2many("country_ids", "res.country", orm.FieldOpts{String: "Countries"}),
	)
}
