package v1

import (
	"quyun/v2/app/errorx"
	"quyun/v2/app/http/v1/dto"
	"quyun/v2/app/requests"
	"quyun/v2/app/services"
	"quyun/v2/database/models"

	"github.com/gofiber/fiber/v3"
)

// @provider
type Creator struct{}

// Apply to become a creator
//
// @Router /t/:tenantCode/v1/creator/apply [post]
// @Summary Apply creator
// @Description Apply to become a creator
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param form body dto.ApplyForm true "Apply form"
// @Success 200 {string} string "Application submitted"
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Creator) Apply(ctx fiber.Ctx, user *models.User, form *dto.ApplyForm) error {
	tenantID := getTenantID(ctx)
	return services.Creator.Apply(ctx, tenantID, user.ID, form)
}

// Review join request
//
// @Router /t/:tenantCode/v1/creator/members/:id<int>/review [post]
// @Summary Review join request
// @Description Approve or reject a tenant join request
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Join request ID"
// @Param form body dto.TenantJoinReviewForm true "Review form"
// @Success 200 {string} string "Reviewed"
// @Bind user local key(__ctx_user)
// @Bind id path
// @Bind form body
func (c *Creator) ReviewMember(ctx fiber.Ctx, user *models.User, id int64, form *dto.TenantJoinReviewForm) error {
	tenantID := getTenantID(ctx)
	return services.Tenant.ReviewJoin(ctx, tenantID, user.ID, id, form)
}

// Create member invite
//
// @Router /t/:tenantCode/v1/creator/members/invite [post]
// @Summary Create member invite
// @Description Create an invite for tenant members
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param form body dto.TenantInviteCreateForm true "Invite form"
// @Success 200 {object} dto.TenantInviteItem
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Creator) CreateMemberInvite(
	ctx fiber.Ctx,
	user *models.User,
	form *dto.TenantInviteCreateForm,
) (*dto.TenantInviteItem, error) {
	tenantID := getTenantID(ctx)
	return services.Tenant.CreateInvite(ctx, tenantID, user.ID, form)
}

// Get report overview
//
// @Router /t/:tenantCode/v1/creator/reports/overview [get]
// @Summary Report overview
// @Description Get creator report overview
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param start_at query string false "Start time (RFC3339)"
// @Param end_at query string false "End time (RFC3339)"
// @Param granularity query string false "Granularity (day)"
// @Success 200 {object} dto.ReportOverviewResponse
// @Bind user local key(__ctx_user)
// @Bind filter query
func (c *Creator) ReportOverview(
	ctx fiber.Ctx,
	user *models.User,
	filter *dto.ReportOverviewFilter,
) (*dto.ReportOverviewResponse, error) {
	tenantID := getTenantID(ctx)
	return services.Creator.ReportOverview(ctx, tenantID, user.ID, filter)
}

// Export report overview
//
// @Router /t/:tenantCode/v1/creator/reports/export [post]
// @Summary Export report overview
// @Description Export creator report overview
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param form body dto.ReportExportForm true "Export form"
// @Success 200 {object} dto.ReportExportResponse
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Creator) ExportReport(
	ctx fiber.Ctx,
	user *models.User,
	form *dto.ReportExportForm,
) (*dto.ReportExportResponse, error) {
	tenantID := getTenantID(ctx)
	return services.Creator.ExportReport(ctx, tenantID, user.ID, form)
}

// Get creator dashboard stats
//
// @Router /t/:tenantCode/v1/creator/dashboard [get]
// @Summary Dashboard stats
// @Description Get creator dashboard stats
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Success 200 {object} dto.DashboardStats
// @Bind user local key(__ctx_user)
func (c *Creator) Dashboard(ctx fiber.Ctx, user *models.User) (*dto.DashboardStats, error) {
	tenantID := getTenantID(ctx)
	return services.Creator.Dashboard(ctx, tenantID, user.ID)
}

// Get content details for edit
//
// @Router /t/:tenantCode/v1/creator/contents/:id<int> [get]
// @Summary Get content
// @Description Get content details for edit
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Content ID"
// @Success 200 {object} dto.ContentEditDTO
// @Bind user local key(__ctx_user)
// @Bind id path
func (c *Creator) GetContent(ctx fiber.Ctx, user *models.User, id int64) (*dto.ContentEditDTO, error) {
	tenantID := getTenantID(ctx)
	return services.Creator.GetContent(ctx, tenantID, user.ID, id)
}

// List creator contents
//
// @Router /t/:tenantCode/v1/creator/contents [get]
// @Summary List contents
// @Description List creator contents
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param status query string false "Status"
// @Param genre query string false "Genre"
// @Param keyword query string false "Keyword"
// @Success 200 {array} dto.ContentItem
// @Bind user local key(__ctx_user)
// @Bind filter query
func (c *Creator) ListContents(
	ctx fiber.Ctx,
	user *models.User,
	filter *dto.CreatorContentListFilter,
) (*requests.Pager, error) {
	tenantID := getTenantID(ctx)
	return services.Creator.ListContents(ctx, tenantID, user.ID, filter)
}

// Create/Publish content
//
// @Router /t/:tenantCode/v1/creator/contents [post]
// @Summary Create content
// @Description Create/Publish content
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param form body dto.ContentCreateForm true "Content form"
// @Success 200 {string} string "Created"
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Creator) CreateContent(ctx fiber.Ctx, user *models.User, form *dto.ContentCreateForm) error {
	tenantID := getTenantID(ctx)
	return services.Creator.CreateContent(ctx, tenantID, user.ID, form)
}

// Update content
//
// @Router /t/:tenantCode/v1/creator/contents/:id<int> [put]
// @Summary Update content
// @Description Update content
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Content ID"
// @Param form body dto.ContentUpdateForm true "Update form"
// @Success 200 {string} string "Updated"
// @Bind user local key(__ctx_user)
// @Bind id path
// @Bind form body
func (c *Creator) UpdateContent(ctx fiber.Ctx, user *models.User, id int64, form *dto.ContentUpdateForm) error {
	tenantID := getTenantID(ctx)
	return services.Creator.UpdateContent(ctx, tenantID, user.ID, id, form)
}

// Delete content
//
// @Router /t/:tenantCode/v1/creator/contents/:id<int> [delete]
// @Summary Delete content
// @Description Delete content
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Content ID"
// @Success 200 {string} string "Deleted"
// @Bind user local key(__ctx_user)
// @Bind id path
func (c *Creator) DeleteContent(ctx fiber.Ctx, user *models.User, id int64) error {
	tenantID := getTenantID(ctx)
	return services.Creator.DeleteContent(ctx, tenantID, user.ID, id)
}

// List sales orders
//
// @Router /t/:tenantCode/v1/creator/orders [get]
// @Summary List sales orders
// @Description List sales orders
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param status query string false "Status"
// @Param keyword query string false "Keyword"
// @Success 200 {array} dto.Order
// @Bind user local key(__ctx_user)
// @Bind filter query
func (c *Creator) ListOrders(
	ctx fiber.Ctx,
	user *models.User,
	filter *dto.CreatorOrderListFilter,
) ([]dto.Order, error) {
	tenantID := getTenantID(ctx)
	return services.Creator.ListOrders(ctx, tenantID, user.ID, filter)
}

// Process refund
//
// @Router /t/:tenantCode/v1/creator/orders/:id<int>/refund [post]
// @Summary Process refund
// @Description Process refund
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Order ID"
// @Param form body dto.RefundForm true "Refund form"
// @Success 200 {string} string "Processed"
// @Bind user local key(__ctx_user)
// @Bind id path
// @Bind form body
func (c *Creator) Refund(ctx fiber.Ctx, user *models.User, id int64, form *dto.RefundForm) error {
	tenantID := getTenantID(ctx)
	return services.Creator.ProcessRefund(ctx, tenantID, user.ID, id, form)
}

// Get channel settings
//
// @Router /t/:tenantCode/v1/creator/settings [get]
// @Summary Get settings
// @Description Get channel settings
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Success 200 {object} dto.Settings
// @Bind user local key(__ctx_user)
func (c *Creator) GetSettings(ctx fiber.Ctx, user *models.User) (*dto.Settings, error) {
	tenantID := getTenantID(ctx)
	return services.Creator.GetSettings(ctx, tenantID, user.ID)
}

// Update channel settings
//
// @Router /t/:tenantCode/v1/creator/settings [put]
// @Summary Update settings
// @Description Update channel settings
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param form body dto.Settings true "Settings form"
// @Success 200 {string} string "Updated"
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Creator) UpdateSettings(ctx fiber.Ctx, user *models.User, form *dto.Settings) error {
	tenantID := getTenantID(ctx)
	return services.Creator.UpdateSettings(ctx, tenantID, user.ID, form)
}

// List payout accounts
//
// @Router /t/:tenantCode/v1/creator/payout-accounts [get]
// @Summary List payout accounts
// @Description List payout accounts
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Success 200 {array} dto.PayoutAccount
// @Bind user local key(__ctx_user)
func (c *Creator) ListPayoutAccounts(ctx fiber.Ctx, user *models.User) ([]dto.PayoutAccount, error) {
	tenantID := getTenantID(ctx)
	return services.Creator.ListPayoutAccounts(ctx, tenantID, user.ID)
}

// Add payout account
//
// @Router /t/:tenantCode/v1/creator/payout-accounts [post]
// @Summary Add payout account
// @Description Add payout account
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param form body dto.PayoutAccount true "Account form"
// @Success 200 {string} string "Added"
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Creator) AddPayoutAccount(ctx fiber.Ctx, user *models.User, form *dto.PayoutAccount) error {
	tenantID := getTenantID(ctx)
	return services.Creator.AddPayoutAccount(ctx, tenantID, user.ID, form)
}

// Remove payout account
//
// @Router /t/:tenantCode/v1/creator/payout-accounts [delete]
// @Summary Remove payout account
// @Description Remove payout account
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id query int64 true "Account ID"
// @Success 200 {string} string "Removed"
// @Bind user local key(__ctx_user)
// @Bind id query
func (c *Creator) RemovePayoutAccount(ctx fiber.Ctx, user *models.User, id int64) error {
	tenantID := getTenantID(ctx)
	return services.Creator.RemovePayoutAccount(ctx, tenantID, user.ID, id)
}

// Request withdrawal
//
// @Router /t/:tenantCode/v1/creator/withdraw [post]
// @Summary Request withdrawal
// @Description Request withdrawal
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param form body dto.WithdrawForm true "Withdraw form"
// @Success 200 {string} string "Withdrawal requested"
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Creator) Withdraw(ctx fiber.Ctx, user *models.User, form *dto.WithdrawForm) error {
	tenantID := getTenantID(ctx)
	return services.Creator.Withdraw(ctx, tenantID, user.ID, form)
}

// Create coupon
//
// @Router /t/:tenantCode/v1/creator/coupons [post]
// @Summary Create coupon
// @Description Create coupon template
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param form body dto.CouponCreateForm true "Coupon form"
// @Success 200 {object} dto.CouponItem
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Creator) CreateCoupon(ctx fiber.Ctx, user *models.User, form *dto.CouponCreateForm) (*dto.CouponItem, error) {
	tenantID := getTenantID(ctx)
	return services.Coupon.Create(ctx, tenantID, user.ID, form)
}

// List coupons
//
// @Router /t/:tenantCode/v1/creator/coupons [get]
// @Summary List coupons
// @Description List coupon templates
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param page query int false "Page"
// @Param limit query int false "Limit"
// @Param type query string false "Type (fix_amount/discount)"
// @Param status query string false "Status (active/expired)"
// @Param keyword query string false "Keyword"
// @Success 200 {object} requests.Pager
// @Bind user local key(__ctx_user)
// @Bind filter query
func (c *Creator) ListCoupons(ctx fiber.Ctx, user *models.User, filter *dto.CouponListFilter) (*requests.Pager, error) {
	tenantID := getTenantID(ctx)
	return services.Coupon.List(ctx, tenantID, filter)
}

// Get coupon
//
// @Router /t/:tenantCode/v1/creator/coupons/:id<int> [get]
// @Summary Get coupon
// @Description Get coupon template detail
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Coupon ID"
// @Success 200 {object} dto.CouponItem
// @Bind user local key(__ctx_user)
// @Bind id path
func (c *Creator) GetCoupon(ctx fiber.Ctx, user *models.User, id int64) (*dto.CouponItem, error) {
	tenantID := getTenantID(ctx)
	return services.Coupon.Get(ctx, tenantID, id)
}

// Update coupon
//
// @Router /t/:tenantCode/v1/creator/coupons/:id<int> [put]
// @Summary Update coupon
// @Description Update coupon template
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Coupon ID"
// @Param form body dto.CouponUpdateForm true "Coupon form"
// @Success 200 {object} dto.CouponItem
// @Bind user local key(__ctx_user)
// @Bind id path
// @Bind form body
func (c *Creator) UpdateCoupon(ctx fiber.Ctx, user *models.User, id int64, form *dto.CouponUpdateForm) (*dto.CouponItem, error) {
	tenantID := getTenantID(ctx)
	return services.Coupon.Update(ctx, tenantID, user.ID, id, form)
}

// Grant coupon
//
// @Router /t/:tenantCode/v1/creator/coupons/:id<int>/grant [post]
// @Summary Grant coupon
// @Description Grant coupon to users
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path int64 true "Coupon ID"
// @Param form body dto.CouponGrantForm true "Grant form"
// @Success 200 {string} string "Granted"
// @Bind user local key(__ctx_user)
// @Bind id path
// @Bind form body
func (c *Creator) GrantCoupon(ctx fiber.Ctx, user *models.User, id int64, form *dto.CouponGrantForm) (string, error) {
	tenantID := getTenantID(ctx)
	if form == nil {
		return "", errorx.ErrInvalidParameter.WithMsg("参数无效")
	}
	_, err := services.Coupon.Grant(ctx, tenantID, id, form.UserIDs)
	if err != nil {
		return "", err
	}
	return "Granted", nil
}
