from fastapi import HTTPException, status
from sqlalchemy.orm import Session
from app.models.user import User
from app.models.group import PluginGroup
from app.schemas.client import CheckUpdateData
from datetime import datetime, timezone

class GroupService:
    def check_update(self, db: Session, username: str) -> CheckUpdateData:
        user = db.query(User).filter(User.username == username).first()
        if not user:
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户不存在")

        # Check expiration
        is_expired = False
        if user.expire_date:
            now = datetime.now(timezone.utc)
            expire_date = user.expire_date
            if expire_date.tzinfo is None:
                 expire_date = expire_date.replace(tzinfo=timezone.utc)
            if now > expire_date:
                is_expired = True

        # Get group and version
        if not user.group_id:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="用户未分配组")
        
        group = db.query(PluginGroup).filter(PluginGroup.id == user.group_id).first()
        if not group:
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户所属组不存在")

        # 允许无版本的情况，或者使用默认版本
        version_file = group.current_version_file
        if not version_file or version_file == '-':
            # 尝试查找任意一个版本，或者返回空
            # 根据用户要求 "现在默认只有一个了"
            # 这里可以列出 archives 目录下的第一个文件
            import os
            try:
                archives_dir = "archives"
                files = [f for f in os.listdir(archives_dir) if f.endswith('.zip')]
                if files:
                    files.sort(reverse=True) # Assuming newer versions sort higher
                    version_file = files[0]
            except:
                pass

        if not version_file or version_file == '-':
             # 如果还是没有，则抛出异常，或者返回空 update
             raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="当前组无可用版本")

        # Construct download URL (assuming base URL or relative path)
        # In a real scenario, this might be from config
        download_url = f"/download/{version_file}"
        
        # Extract version from filename roughly or use file metadata if available
        # Assuming filename format: plugin_v1.0.2.zip
        version = "unknown"
        filename = version_file
        if "v" in filename:
            try:
                # Simple extraction logic, can be improved
                parts = filename.split("v")
                if len(parts) > 1:
                    version_part = parts[1]
                    version = "v" + version_part.split(".zip")[0].split("_")[0]
            except:
                pass

        return CheckUpdateData(
            version=version,
            download_url=download_url,
            force_update=False,
            is_expired=is_expired
        )

group_service = GroupService()
