"""俱乐部维度财务统计（供 /jituan/houtai/caiwu，旧 /houtai/caiwu 不变）。"""
from datetime import date, datetime, timedelta

from django.db.models import Count, Q, Sum

from jituan.constants import DATA_SCOPE_ALL
from jituan.services.club_context import (
    filter_club_char_field,
    filter_queryset_by_club,
    resolve_club_id_from_request,
    resolve_club_scope,
)
from jituan.services.daily_finance_display import (
    cumulative_income_payout,
    daily_stats_for_request,
    sum_payout_stat_for_date,
    today_income_for_dashboard,
)

from orders.models import Order
from products.models import Czjilu, Huiyuangoumai
from users.models import UserDashou, UserGuanshi, UserShangjia, UserZuzhang


def _role_profile_qs(profile_model, request):
    qs = profile_model.query.all()
    if resolve_club_scope(request) == DATA_SCOPE_ALL:
        return qs
    return qs.filter(user__ClubID=resolve_club_id_from_request(request))


def _sum_role_balance(profile_model, balance_field, request):
    return float(
        _role_profile_qs(profile_model, request).aggregate(total=Sum(balance_field))['total'] or 0.00
    )


def _order_qs(request):
    return filter_queryset_by_club(Order.query.all(), request, club_field='ClubID')


def _czjilu_qs(request):
    return filter_club_char_field(Czjilu.query.all(), request, field='club_id')


def _huiyuangoumai_qs(request):
    return filter_club_char_field(Huiyuangoumai.query.all(), request, field='club_id')


def build_caiwu_payload(request):
    """与 CaiwuView 一致；按 Header 俱乐部范围读 daily_income_stat、daily_payout_stat。"""
    today = date.today()
    today_start = datetime.combine(today, datetime.min.time())
    today_end = today_start + timedelta(days=1)
    club_id = resolve_club_id_from_request(request)

    order_qs = _order_qs(request)
    cz_qs = _czjilu_qs(request)
    hy_qs = _huiyuangoumai_qs(request)

    today_income_amount, today_income_count = today_income_for_dashboard(today, request)
    today_payout_amount, today_payout_count = sum_payout_stat_for_date(today, request)

    # 合并订单统计：6 次独立 count/aggregate → 1 次 aggregate
    _today_order_agg = order_qs.filter(
        CreateTime__gte=today_start,
        CreateTime__lt=today_end,
        Status__in=[1, 2, 3, 4, 5, 6, 7, 8],
    ).aggregate(
        today_order_count=Count('id'),
        today_order_amount=Sum('Amount'),
        today_completed_count=Count('id', filter=Q(Status=3)),
        today_completed_amount=Sum('Amount', filter=Q(Status=3)),
        today_tuikuan_count=Count('id', filter=Q(Status=5)),
        today_tuikuan_amount=Sum('Amount', filter=Q(Status=5)),
    )
    today_order_count = _today_order_agg['today_order_count'] or 0
    today_order_amount = float(_today_order_agg['today_order_amount'] or 0.00)
    today_completed_count = _today_order_agg['today_completed_count'] or 0
    today_completed_amount = float(_today_order_agg['today_completed_amount'] or 0.00)
    today_tuikuan_count = _today_order_agg['today_tuikuan_count'] or 0
    today_tuikuan_amount = float(_today_order_agg['today_tuikuan_amount'] or 0.00)

    # 合并充值统计：2 次独立 count/aggregate → 1 次 aggregate
    _today_chongzhi_agg = cz_qs.filter(
        CreateTime__gte=today_start, CreateTime__lt=today_end, leixing=1, zhuangtai=3,
    ).aggregate(
        count=Count('id'),
        total=Sum('jine'),
    )
    today_chongzhi_count = _today_chongzhi_agg['count'] or 0
    today_chongzhi_amount = float(_today_chongzhi_agg['total'] or 0.00)

    new_huiyuan_user_ids = hy_qs.filter(
        CreateTime__gte=today_start, CreateTime__lt=today_end,
    ).values_list('yonghu_id', flat=True).distinct()
    today_new_huiyuan = len(list(new_huiyuan_user_ids))

    today_new_huiyuan_amount = 0.0
    if new_huiyuan_user_ids:
        amount_sum = cz_qs.filter(
            CreateTime__gte=today_start,
            CreateTime__lt=today_end,
            yonghuid__in=new_huiyuan_user_ids,
            leixing=1,
            zhuangtai=3,
        ).aggregate(total=Sum('jine'))
        today_new_huiyuan_amount = float(amount_sum['total'] or 0.0)

    all_guanshi_yue = _sum_role_balance(UserGuanshi, 'yue', request)
    all_dashou_yue = _sum_role_balance(UserDashou, 'yue', request)
    all_zuzhang_yue = _sum_role_balance(UserZuzhang, 'ketixian_jine', request)
    all_shangjia_yue = _sum_role_balance(UserShangjia, 'yue', request)

    total_income, total_payout, platform_profit = cumulative_income_payout(request)
    daily_stats = daily_stats_for_request(request, days=30)

    return {
        'club_id': club_id,
        'scope': resolve_club_scope(request),
        'today_income': round(today_income_amount, 2),
        'today_income_count': today_income_count,
        'today_payout': round(today_payout_amount, 2),
        'today_payout_count': today_payout_count,
        'today_order_count': today_order_count,
        'today_order_amount': round(today_order_amount, 2),
        'today_completed_count': today_completed_count,
        'today_completed_amount': round(today_completed_amount, 2),
        'today_tuikuan_count': today_tuikuan_count,
        'today_tuikuan_amount': round(today_tuikuan_amount, 2),
        'today_chongzhi_count': today_chongzhi_count,
        'today_chongzhi_amount': round(today_chongzhi_amount, 2),
        'today_new_huiyuan': today_new_huiyuan,
        'today_new_huiyuan_amount': round(today_new_huiyuan_amount, 2),
        'all_guanshi_yue': round(all_guanshi_yue, 2),
        'all_dashou_yue': round(all_dashou_yue, 2),
        'all_zuzhang_yue': round(all_zuzhang_yue, 2),
        'all_shangjia_yue': round(all_shangjia_yue, 2),
        'total_income': round(total_income, 2),
        'total_payout': round(total_payout, 2),
        'platform_profit': platform_profit,
        'daily_stats': daily_stats,
    }
