package forms

import (
	"time"

	"github.com/shopspring/decimal"
)

// === Admin API (Campaign management) ===

type CreateCampaignReq struct {
	Name              string          `json:"name" binding:"required"`
	TotalBudget       decimal.Decimal `json:"total_budget" binding:"required"`
	Note              string          `json:"note"`
	IsWebsiteCampaign bool            `json:"is_website_campaign"`
}

type CampaignResponse struct {
	ID                uint            `json:"id"`
	Name              string          `json:"name"`
	Status            string          `json:"status"`
	TotalBudget       decimal.Decimal `json:"total_budget"`
	UsedBudget        decimal.Decimal `json:"used_budget"`
	RemainingBudget   decimal.Decimal `json:"remaining_budget"`
	CreatedAt         time.Time       `json:"created_at"`
	CreatedBy         string          `json:"created_by"`
	Note              string          `json:"note"`
	IsWebsiteCampaign bool            `json:"is_website_campaign"`
}

type CampaignListResponse struct {
	Campaigns      []CampaignResponse `json:"campaigns"`
	TotalBudgetAll decimal.Decimal    `json:"total_budget_all"`
	UsedBudgetAll  decimal.Decimal    `json:"used_budget_all"`
	RemainingAll   decimal.Decimal    `json:"remaining_all"`
}

// === Inner API (External service triggers reward) ===

type ReferralRewardReq struct {
	ReferralCode    string          `json:"referral_code" binding:"required"`
	RefereeTenantID uint            `json:"referee_tenant_id"`
	EventType       string          `json:"event_type" binding:"required"`
	CreditsAmount   decimal.Decimal `json:"credits_amount"`
}

type ReferralRewardResp struct {
	Rewarded     bool            `json:"rewarded"`
	RewardAmount decimal.Decimal `json:"reward_amount,omitempty"`
	CreditsCode  string          `json:"credits_code,omitempty"`
	SkipReason   string          `json:"skip_reason,omitempty"`
}

// === User API ===

type ReferralCodeResponse struct {
	CampaignID    uint            `json:"campaign_id"`
	CampaignName  string          `json:"campaign_name"`
	Code          string          `json:"code"`
	ReferralCount int             `json:"referral_count"`
	TotalEarned   decimal.Decimal `json:"total_earned"`
	ShareLink     string          `json:"share_link"`
}

type ReferralEventItem struct {
	RefereeTenantID uint            `json:"referee_tenant_id"`
	EventType       string          `json:"event_type"`
	Rewarded        bool            `json:"rewarded"`
	RewardAmount    decimal.Decimal `json:"reward_amount"`
	SkipReason      string          `json:"skip_reason"`
	CreatedAt       time.Time       `json:"created_at"`
}

type ReferralStatsResponse struct {
	TotalReferrals int                    `json:"total_referrals"`
	TotalEarned    decimal.Decimal        `json:"total_earned"`
	Codes          []ReferralCodeResponse `json:"codes"`
	Events         []ReferralEventItem    `json:"events"`
}

// === Invoice Paid Event ===

type InvoicePaidEvent struct {
	TenantID        uint            `json:"tenant_id"`
	BillingConfigID uint            `json:"billing_config_id"`
	PaidAmount      decimal.Decimal `json:"paid_amount"`
}

// === Reward config (global, loaded from config file) ===

type ReferralRewardConfig struct {
	CreditsValidityDays int    `json:"credits_validity_days"`
	Usage               string `json:"usage"`
}

type NotifyRegistrationReq struct {
	TenantID                  uint   `json:"tenant_id" binding:"required"`
	WebsiteReferrerUrl        string `json:"website_referrer_url"`
	UtmCampaign               string `json:"utm_campaign"`
	UtmContent                string `json:"utm_content"`
	UtmMedium                 string `json:"utm_medium"`
	UtmSource                 string `json:"utm_source"`
	UtmTerm                   string `json:"utm_term"`
	MostRecentReferralUrl     string `json:"most_recent_referral_url"`
	SignupSource              string `json:"signup_source"`
	ProviderSource            string `json:"provider_source"`
	ThirdPartyAccountProvider string `json:"third_party_account_provider"`
	ThirdPartyAccountCode     string `json:"third_party_account_code"`
}

type TenantPromotionResponse struct {
	TenantID    uint `json:"tenant_id"`
	IsPromotion bool `json:"is_promotion"`
}
