from odoo import models, fields, api

class ShopifyLocationWizard(models.TransientModel):
    _name = 'shopify.location.wizard'
    _description = 'Selector de Ubicaciones Shopify'

    config_id = fields.Many2one('shopify.config', string='Configuración', required=True)
    line_ids = fields.One2many('shopify.location.wizard.line', 'wizard_id', string='Sucursales Disponibles')

    def action_confirm(self):
        self.ensure_one()
        selected_line = self.line_ids.filtered(lambda l: l.is_selected)
        if not selected_line:
            from odoo.exceptions import UserError
            raise UserError('Por favor, selecciona una sucursal.')
        
        if len(selected_line) > 1:
            from odoo.exceptions import UserError
            raise UserError('Solo puedes seleccionar una sucursal.')

        self.config_id.write({
            'shopify_location_id': selected_line.shopify_location_id,
            'shopify_location_name': selected_line.name
        })
        return {'type': 'ir.actions.act_window_close'}

class ShopifyLocationWizardLine(models.TransientModel):
    _name = 'shopify.location.wizard.line'
    _description = 'Línea de Ubicación Shopify'

    wizard_id = fields.Many2one('shopify.location.wizard', string='Wizard')
    shopify_location_id = fields.Char(string='ID Shopify', readonly=True)
    name = fields.Char(string='Nombre Sucursal', readonly=True)
    is_selected = fields.Boolean(string='Seleccionada')
