package v1

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

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

// @provider
type Creator struct{}

// Apply to become a creator
//
// @Router /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 {
	return services.Creator.Apply(ctx, user.ID, form)
}

// Get creator dashboard stats
//
// @Router /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) {
	return services.Creator.Dashboard(ctx, user.ID)
}

// Get content details for edit
//
// @Router /v1/creator/contents/:id [get]
// @Summary Get content
// @Description Get content details for edit
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path string 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 string) (*dto.ContentEditDTO, error) {
	return services.Creator.GetContent(ctx, user.ID, id)
}

// List creator contents
//
// @Router /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,
) ([]dto.CreatorContentItem, error) {
	return services.Creator.ListContents(ctx, user.ID, filter)
}

// Create/Publish content
//
// @Router /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 {
	return services.Creator.CreateContent(ctx, user.ID, form)
}

// Update content
//
// @Router /v1/creator/contents/:id [put]
// @Summary Update content
// @Description Update content
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path string 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 string, form *dto.ContentUpdateForm) error {
	return services.Creator.UpdateContent(ctx, user.ID, id, form)
}

// Delete content
//
// @Router /v1/creator/contents/:id [delete]
// @Summary Delete content
// @Description Delete content
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path string 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 string) error {
	return services.Creator.DeleteContent(ctx, user.ID, id)
}

// List sales orders
//
// @Router /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) {
	return services.Creator.ListOrders(ctx, user.ID, filter)
}

// Process refund
//
// @Router /v1/creator/orders/:id/refund [post]
// @Summary Process refund
// @Description Process refund
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id path string 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 string, form *dto.RefundForm) error {
	return services.Creator.ProcessRefund(ctx, user.ID, id, form)
}

// Get channel settings
//
// @Router /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) {
	return services.Creator.GetSettings(ctx, user.ID)
}

// Update channel settings
//
// @Router /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 {
	return services.Creator.UpdateSettings(ctx, user.ID, form)
}

// List payout accounts
//
// @Router /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) {
	return services.Creator.ListPayoutAccounts(ctx, user.ID)
}

// Add payout account
//
// @Router /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 {
	return services.Creator.AddPayoutAccount(ctx, user.ID, form)
}

// Remove payout account
//
// @Router /v1/creator/payout-accounts [delete]
// @Summary Remove payout account
// @Description Remove payout account
// @Tags CreatorCenter
// @Accept json
// @Produce json
// @Param id query string 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 string) error {
	return services.Creator.RemovePayoutAccount(ctx, user.ID, id)
}

// Request withdrawal
//
// @Router /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 {
	return services.Creator.Withdraw(ctx, user.ID, form)
}
