#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
接口定义历史模型
对应interfacedefhistory表
"""

import datetime
from sqlalchemy import Column, String, Integer, DateTime, UniqueConstraint, Index
from sqlalchemy.dialects.mysql import LONGTEXT
from data.models.base import BaseModel


class InterfaceDefHistory(BaseModel):
    """
    接口定义历史模型
    对应interfacedefhistory表
    功能：存储系统接口的定义和版本历史
    """
    __tablename__ = 'interfacedefhistory'

    # 唯一约束和索引
    __table_args__ = (
        UniqueConstraint('method', 'url', 'version', name='uniq'),
        Index('idx_interfacedefhistory_method', 'method'),
        Index('idx_interfacedefhistory_url', 'url'),
        Index('idx_interfacedefhistory_version', 'version'),
        Index('idx_interfacedefhistory_project_id', 'project_id'),
        Index('idx_interfacedefhistory_created_at', 'created_at'),
        {
            'mysql_engine': 'InnoDB',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_general_ci',
            'info': {'order_by': 'created_at DESC'}
        }
    )
    
    id = Column(String(255), primary_key=True, nullable=False, comment='接口定义历史记录ID')
    create_date = Column(DateTime, default=datetime.datetime.now, comment='创建日期')
    detail = Column(LONGTEXT, comment='接口详细定义（JSON格式）')
    method = Column(String(255), nullable=False, comment='请求方法(GET, POST, PUT, DELETE等)')
    project_id = Column(String(255), comment='关联的项目ID')
    url = Column(String(255), nullable=False, comment='接口URL')
    version = Column(Integer, comment='版本号')
    
    def __repr__(self):
        return f"<InterfaceDefHistory(id='{self.id}', method='{self.method}', url='{self.url}', version='{self.version}')>" 