package dto

import (
	v1_dto "quyun/v2/app/http/v1/dto"
	"quyun/v2/app/requests"
	"quyun/v2/pkg/consts"
)

// Filters
type UserListFilter struct {
	requests.Pagination
	Username *string `query:"username"`
}

type TenantListFilter struct {
	requests.Pagination
	Name *string `query:"name"`
}

type SuperContentListFilter struct {
	requests.Pagination
	// Add filters if needed, currently list signature is just pagination in super.go service
}

type SuperOrderListFilter struct {
	requests.Pagination
	// Add filters if needed
}

// SuperUserLite 用于平台用户列表的轻量级用户信息
type SuperUserLite struct {
	ID                int64             `json:"id"`
	Username          string            `json:"username"`
	Roles             []consts.Role     `json:"roles"`
	Status            consts.UserStatus `json:"status"`
	StatusDescription string            `json:"status_description"`
	VerifiedAt        string            `json:"verified_at"`
	CreatedAt         string            `json:"created_at"`
	UpdatedAt         string            `json:"updated_at"`
}

type UserItem struct {
	SuperUserLite
	Balance           int64 `json:"balance"`
	BalanceFrozen     int64 `json:"balance_frozen"`
	OwnedTenantCount  int64 `json:"owned_tenant_count"`
	JoinedTenantCount int64 `json:"joined_tenant_count"`
}

type UserStatistics struct {
	Status            consts.UserStatus `json:"status"`
	StatusDescription string            `json:"status_description"`
	Count             int64             `json:"count"`
}

type UserStatusUpdateForm struct {
	Status consts.UserStatus `json:"status" validate:"required"`
}

type UserRolesUpdateForm struct {
	Roles []consts.Role `json:"roles" validate:"required,min=1"`
}

type UserTenantItem struct {
	TenantID                int64                   `json:"tenant_id"`
	TenantStatus            consts.TenantStatus     `json:"tenant_status"`
	TenantStatusDescription string                  `json:"tenant_status_description"`
	Name                    string                  `json:"name"`
	Code                    string                  `json:"code"`
	Owner                   *TenantOwnerUserLite    `json:"owner"`
	Role                    []consts.TenantUserRole `json:"role"`
	MemberStatus            consts.UserStatus       `json:"member_status"`
	MemberStatusDescription string                  `json:"member_status_description"`
	JoinedAt                string                  `json:"joined_at"`
	ExpiredAt               string                  `json:"expired_at"`
}

// Tenant Related
type TenantCreateForm struct {
	Name        string `json:"name" validate:"required,max=128"`
	Code        string `json:"code" validate:"required,max=64"`
	AdminUserID int64  `json:"admin_user_id" validate:"required"`
	Duration    int    `json:"duration" validate:"required,oneof=7 30 90 180 365"`
}

type TenantItem struct {
	ID                  int64                  `json:"id"`
	UUID                string                 `json:"uuid"`
	Code                string                 `json:"code"`
	Name                string                 `json:"name"`
	Status              consts.TenantStatus    `json:"status"`
	StatusDescription   string                 `json:"status_description"`
	Config              []int                  `json:"config"` // Replace with actual config struct if needed
	Owner               *TenantOwnerUserLite   `json:"owner"`
	AdminUsers          []*TenantAdminUserLite `json:"admin_users"`
	UserCount           int64                  `json:"user_count"`
	IncomeAmountPaidSum int64                  `json:"income_amount_paid_sum"`
	ExpiredAt           string                 `json:"expired_at"`
	CreatedAt           string                 `json:"created_at"`
	UpdatedAt           string                 `json:"updated_at"`
	UserID              int64                  `json:"user_id"`
	Users               []*SuperUserLite       `json:"users"`
}

type TenantOwnerUserLite struct {
	ID       int64  `json:"id"`
	Username string `json:"username"`
}

type TenantAdminUserLite struct {
	ID       int64  `json:"id"`
	Username string `json:"username"`
}

type TenantExpireUpdateForm struct {
	Duration int `json:"duration" validate:"required,oneof=7 30 90 180 365"`
}

type TenantStatusUpdateForm struct {
	Status consts.TenantStatus `json:"status" validate:"required"`
}

type SuperTenantContentItem struct {
	Content               *v1_dto.ContentItem     `json:"content"`
	StatusDescription     string                  `json:"status_description"`
	VisibilityDescription string                  `json:"visibility_description"`
	Tenant                *SuperContentTenantLite `json:"tenant"`
	Owner                 *SuperUserLite          `json:"owner"`
	Price                 *v1_dto.ContentPrice    `json:"price"`
}

type SuperContentTenantLite struct {
	ID   int64  `json:"id"`
	Code string `json:"code"`
	Name string `json:"name"`
}

type SuperTenantContentStatusUpdateForm struct {
	Status consts.ContentStatus `json:"status" validate:"required,oneof=unpublished blocked"`
}

type SuperTenantUserItem struct {
	User       *SuperUserLite `json:"user"`
	TenantUser *TenantUser    `json:"tenant_user"`
}

type TenantUser struct {
	ID        int64                   `json:"id"`
	TenantID  int64                   `json:"tenant_id"`
	UserID    int64                   `json:"user_id"`
	Role      []consts.TenantUserRole `json:"role"`
	Status    consts.UserStatus       `json:"status"`
	CreatedAt string                  `json:"created_at"`
	UpdatedAt string                  `json:"updated_at"`
}

// Order Related
type SuperOrderItem struct {
	ID                int64              `json:"id"`
	Type              consts.OrderType   `json:"type"`
	Status            consts.OrderStatus `json:"status"`
	StatusDescription string             `json:"status_description"`
	Currency          consts.Currency    `json:"currency"`
	AmountOriginal    int64              `json:"amount_original"`
	AmountDiscount    int64              `json:"amount_discount"`
	AmountPaid        int64              `json:"amount_paid"`
	Tenant            *OrderTenantLite   `json:"tenant"`
	Buyer             *OrderBuyerLite    `json:"buyer"`
	PaidAt            string             `json:"paid_at"`
	RefundedAt        string             `json:"refunded_at"`
	CreatedAt         string             `json:"created_at"`
	UpdatedAt         string             `json:"updated_at"`
}

type OrderTenantLite struct {
	ID   int64  `json:"id"`
	Code string `json:"code"`
	Name string `json:"name"`
}

type OrderBuyerLite struct {
	ID       int64  `json:"id"`
	Username string `json:"username"`
}

type OrderStatisticsResponse struct {
	TotalCount         int64                `json:"total_count"`
	TotalAmountPaidSum int64                `json:"total_amount_paid_sum"`
	ByStatus           []OrderStatisticsRow `json:"by_status"`
}

type OrderStatisticsRow struct {
	Status            consts.OrderStatus `json:"status"`
	StatusDescription string             `json:"status_description"`
	Count             int64              `json:"count"`
	AmountPaidSum     int64              `json:"amount_paid_sum"`
}

type SuperOrderDetail struct {
	Order  *SuperOrderItem  `json:"order"` // Using SuperOrderItem as base, extend if needed
	Tenant *OrderTenantLite `json:"tenant"`
	Buyer  *OrderBuyerLite  `json:"buyer"`
}

type SuperOrderRefundForm struct {
	Force          bool   `json:"force"`
	Reason         string `json:"reason"`
	IdempotencyKey string `json:"idempotency_key"`
}

// AdminContentItem for super admin view
type AdminContentItem struct {
	Content               *v1_dto.ContentItem    `json:"content"`
	Owner                 *AdminContentOwnerLite `json:"owner"`
	Price                 *v1_dto.ContentPrice   `json:"price"`
	StatusDescription     string                 `json:"status_description"`
	VisibilityDescription string                 `json:"visibility_description"`
}

type AdminContentOwnerLite struct {
	ID       int64             `json:"id"`
	Username string            `json:"username"`
	Roles    []consts.Role     `json:"roles"`
	Status   consts.UserStatus `json:"status"`
}
