"""Database models for custom profile fields."""

from indico.core.db import db
from indico.modules.users.models.users import User
from indico.util.string import format_repr


class UserCustomProfile(db.Model):
    """Model for storing custom user profile fields."""

    __tablename__ = "user_custom_profiles"
    __table_args__ = (
        db.UniqueConstraint("user_id"),
        {"schema": "plugin_custom_profile_fields"},
    )

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(
        db.Integer, db.ForeignKey("users.users.id"), nullable=False, index=True
    )

    # Professional Information
    testfield = db.Column(db.String, nullable=True)  # Test field

    # Relationships
    user = db.relationship(
        "User",
        lazy=True,
        backref=db.backref(
            "custom_profile", lazy=True, uselist=False, cascade="all, delete-orphan"
        ),
    )

    def __repr__(self):
        """String representation."""
        return format_repr(
            self,
            "id",
            "user_id",
            "testfield",
            _text=self.user.full_name if self.user else None,
        )

    @classmethod
    def get_for_user(cls, user):
        """Get custom profile for a user, creating if necessary."""
        if isinstance(user, int):
            user = User.get(user)

        if not user:
            return None

        profile = cls.query.filter_by(user_id=user.id).first()
        if not profile:
            profile = cls(user=user)
        return profile

    # def has_data(self):
    #     """Check if any custom fields have data."""
    #     fields = [self.country]
    #     return any(field for field in fields if field)

    # def to_dict(self):
    #     """Convert to dictionary for API/export."""
    #     return {
    #         "country": self.country,
    #     }

    # def update_from_dict(self, data):
    #     """Update fields from dictionary."""
    #     for field in [
    #         "country",
    #     ]:
    #         if field in data:
    #             setattr(self, field, data[field] or None)
