from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy import Table, MetaData
from sqlalchemy.orm import Session
from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime, date
from app.core.database import get_db, engine
from app.models.models import User
from app.routers.auth import get_current_user

router = APIRouter(prefix="/api/news", tags=["资讯"])
metadata = MetaData()

# 分类表
categories_table = Table('news_categories', metadata, autoload_with=engine)
news_table = Table('news', metadata, autoload_with=engine)
user_posts_table = Table('user_posts', metadata, autoload_with=engine)
users_table = Table('users', metadata, autoload_with=engine)
deals_table = Table('deals', metadata, autoload_with=engine)
notifications_table = Table('notifications', metadata, autoload_with=engine)

# ============ 获取分类 ============
@router.get("/categories")
def get_categories(db: Session = Depends(get_db)):
    results = db.query(categories_table).order_by(categories_table.c.sort_order).all()
    return [dict(r._mapping) for r in results]

# ============ 获取资讯 ============
@router.get("")
def get_news(
    category_id: Optional[int] = None,
    page: int = 1,
    limit: int = 20,
    db: Session = Depends(get_db)
):
    query = db.query(news_table)
    if category_id:
        query = query.filter(news_table.c.category_id == category_id)
    offset = (page - 1) * limit
    results = query.order_by(news_table.c.created_at.desc()).offset(offset).limit(limit).all()
    return [dict(r._mapping) for r in results]

# ============ 获取用户发布 ============
@router.get("/posts")
def get_posts(
    post_type: Optional[str] = None,
    status: str = "active",
    page: int = 1,
    limit: int = 20,
    db: Session = Depends(get_db)
):
    query = db.query(user_posts_table).filter(user_posts_table.c.status == status)
    if post_type:
        query = query.filter(user_posts_table.c.post_type == post_type)
    offset = (page - 1) * limit
    results = query.order_by(user_posts_table.c.created_at.desc()).offset(offset).limit(limit).all()
    return [dict(r._mapping) for r in results]

# ============ 创建发布 ============
class PostCreate(BaseModel):
    post_type: str
    title: str
    content: Optional[str] = None
    zodiac_type: Optional[str] = None
    packaging: Optional[str] = None

@router.post("/posts")
def create_post(
    post: PostCreate,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    result = db.execute(user_posts_table.insert().values(
        user_id=current_user.f99_90_id,
        post_type=post.post_type,
        title=post.title,
        content=post.content,
        zodiac_type=post.zodiac_type,
        packaging=post.packaging,
        status="pending"
    ))
    db.commit()
    return {"success": True, "id": result.inserted_primary_key[0]}

# ============ 成交数据 ============
@router.get("/deals")
def get_deals(
    zodiac_type: Optional[str] = None,
    limit: int = 20,
    db: Session = Depends(get_db)
):
    query = db.query(deals_table)
    if zodiac_type:
        query = query.filter(deals_table.c.zodiac_type == zodiac_type)
    results = query.order_by(deals_table.c.deal_date.desc()).limit(limit).all()
    return [dict(r._mapping) for r in results]

# ============ 通知 ============
@router.get("/notifications")
def get_notifications(limit: int = 10, db: Session = Depends(get_db)):
    results = db.query(notifications_table).filter(
        notifications_table.c.is_published == True
    ).order_by(notifications_table.c.created_at.desc()).limit(limit).all()
    return [dict(r._mapping) for r in results]

# ============ 首页数据 ============
@router.get("/home")
def get_home(db: Session = Depends(get_db)):
    # 推荐发布
    posts = db.query(user_posts_table).filter(
        user_posts_table.c.status == "active"
    ).order_by(user_posts_table.c.created_at.desc()).limit(10).all()
    
    # 成交
    deals = db.query(deals_table).order_by(
        deals_table.c.deal_date.desc()
    ).limit(10).all()
    
    # 通知
    notices = db.query(notifications_table).filter(
        notifications_table.c.is_published == True
    ).order_by(notifications_table.c.created_at.desc()).limit(5).all()
    
    return {
        "posts": [dict(p._mapping) for p in posts],
        "deals": [dict(d._mapping) for d in deals],
        "notices": [dict(n._mapping) for n in notices]
    }
