# -*- coding: utf-8 -*-

from odoo import models, fields, api


class PdfFieldPosition(models.Model):
    """PDF Field Position Configuration
    
    Stores user-configurable positions for text overlay on flattened PDF templates.
    Each record defines where a specific field should be placed on a PDF template.
    """
    _name = 'pdf.field.position'
    _description = 'PDF Field Position'
    _order = 'template_type, sequence, field_name'
    _rec_name = 'field_name'

    template_type = fields.Selection([
        ('T4', 'T4 Slip'),
        ('T4 Summary', 'T4 Summary'),
        ('T4A', 'T4A Slip'),
        ('T4A Summary', 'T4A Summary'),
    ], string='Template Type', required=True, index=True,
       help='The PDF template type this field position applies to')

    field_name = fields.Char(
        string='Field Name',
        required=True,
        index=True,
        help='Field identifier used in PDF field mapping (e.g., EmployeeLastName, SIN, Box14)'
    )

    field_label = fields.Char(
        string='Field Label',
        help='Human-readable label for this field (for display purposes)'
    )

    x_position = fields.Float(
        string='X Position',
        required=True,
        default=0.0,
        help='X coordinate in points (1 point = 1/72 inch). Origin is at bottom-left corner.'
    )

    y_position = fields.Float(
        string='Y Position',
        required=True,
        default=0.0,
        help='Y coordinate in points (1 point = 1/72 inch). Origin is at bottom-left corner. Standard letter size is 612 x 792 points.'
    )

    font_size = fields.Integer(
        string='Font Size',
        required=True,
        default=10,
        help='Font size in points (default: 10)'
    )

    font_name = fields.Char(
        string='Font Name',
        required=True,
        default='Helvetica',
        help='Font family name (e.g., Helvetica, Times-Roman, Courier)'
    )

    active = fields.Boolean(
        string='Active',
        default=True,
        help='If unchecked, this field position will not be used when filling PDFs'
    )

    sequence = fields.Integer(
        string='Sequence',
        default=10,
        help='Display order for this field'
    )

    @api.model
    def get_coordinates_dict(self, template_type):
        """Get coordinates dictionary for a template type.
        
        Returns dict in format: {'field_name': (x, y, font_size, font_name)}
        Only includes active positions.
        """
        positions = self.search([
            ('template_type', '=', template_type),
            ('active', '=', True),
        ])
        
        result = {}
        for pos in positions:
            result[pos.field_name] = (
                pos.x_position,
                pos.y_position,
                pos.font_size,
                pos.font_name,
            )
        return result
