#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
钉钉同步日志模型
- DingtalkSyncLog: 全量同步任务日志
- DingtalkStreamEventLog: Stream 增量事件日志
"""
from sqlalchemy import Column, String, Text, Integer, DateTime

from app.base_model import BaseModel


class DingtalkSyncLog(BaseModel):
    """
    钉钉全量同步日志

    字段说明：
    - sync_type: 同步类型 dept=部门 user=用户
    - total_count: 钉钉侧总数
    - success_count: 同步成功数
    - fail_count: 同步失败数
    - status: 同步状态 running/success/partial/failed
    - error_detail: 失败详情（JSON）
    - started_at: 同步开始时间
    - finished_at: 同步结束时间
    """
    __tablename__ = "core_dingtalk_sync_log"

    sync_type = Column(String(20), nullable=False, index=True, comment="同步类型: dept/user")
    total_count = Column(Integer, default=0, comment="总数")
    success_count = Column(Integer, default=0, comment="成功数")
    fail_count = Column(Integer, default=0, comment="失败数")
    status = Column(String(20), default="running", index=True, comment="状态: running/success/partial/failed")
    error_detail = Column(Text, nullable=True, comment="失败详情")
    started_at = Column(DateTime, nullable=True, comment="开始时间")
    finished_at = Column(DateTime, nullable=True, comment="结束时间")

    def __repr__(self):
        return f"<DingtalkSyncLog {self.sync_type} {self.status}>"


class DingtalkStreamEventLog(BaseModel):
    """
    钉钉 Stream 增量事件日志

    记录每一次 Stream 推送的增量变更（部门/用户的创建、修改、删除等）
    """
    __tablename__ = "core_dingtalk_stream_event_log"

    event_type = Column(String(50), nullable=False, index=True, comment="事件类型: org_dept_create/user_add_org等")
    target_type = Column(String(20), nullable=False, index=True, comment="目标类型: dept/user")
    target_name = Column(String(100), nullable=True, comment="目标名称")
    dingtalk_dept_id = Column(String(50), nullable=True, comment="钉钉部门ID")
    dingtalk_userid = Column(String(100), nullable=True, comment="钉钉用户ID")
    status = Column(String(20), default="success", index=True, comment="状态: success/failed")
    error_detail = Column(Text, nullable=True, comment="失败详情")
    event_time = Column(DateTime, nullable=True, comment="事件时间")

    def __repr__(self):
        return f"<DingtalkStreamEventLog {self.event_type} {self.target_type} {self.status}>"
