// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: referal.sql

package dbgen

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const CreateReferral = `-- name: CreateReferral :one
INSERT INTO referrals (
    referral_code,
    referrer_id,
    status,
    reward_amount,
    expires_at
) VALUES (
    $1, $2, $3, $4, $5
) RETURNING id, referral_code, referrer_id, referred_id, status, reward_amount, cashback_amount, created_at, updated_at, expires_at
`

type CreateReferralParams struct {
	ReferralCode string             `json:"referral_code"`
	ReferrerID   string             `json:"referrer_id"`
	Status       Referralstatus     `json:"status"`
	RewardAmount pgtype.Numeric     `json:"reward_amount"`
	ExpiresAt    pgtype.Timestamptz `json:"expires_at"`
}

func (q *Queries) CreateReferral(ctx context.Context, arg CreateReferralParams) (Referral, error) {
	row := q.db.QueryRow(ctx, CreateReferral,
		arg.ReferralCode,
		arg.ReferrerID,
		arg.Status,
		arg.RewardAmount,
		arg.ExpiresAt,
	)
	var i Referral
	err := row.Scan(
		&i.ID,
		&i.ReferralCode,
		&i.ReferrerID,
		&i.ReferredID,
		&i.Status,
		&i.RewardAmount,
		&i.CashbackAmount,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExpiresAt,
	)
	return i, err
}

const CreateReferralSettings = `-- name: CreateReferralSettings :one
INSERT INTO referral_settings (
    referral_reward_amount,
    cashback_percentage,
    max_referrals,
    bet_referral_bonus_percentage,
    expires_after_days,
    updated_by
) VALUES (
    $1, $2, $3, $4, $5, $6
) RETURNING id, referral_reward_amount, cashback_percentage, bet_referral_bonus_percentage, max_referrals, expires_after_days, updated_by, created_at, updated_at, version
`

type CreateReferralSettingsParams struct {
	ReferralRewardAmount       pgtype.Numeric `json:"referral_reward_amount"`
	CashbackPercentage         pgtype.Numeric `json:"cashback_percentage"`
	MaxReferrals               int32          `json:"max_referrals"`
	BetReferralBonusPercentage pgtype.Numeric `json:"bet_referral_bonus_percentage"`
	ExpiresAfterDays           int32          `json:"expires_after_days"`
	UpdatedBy                  string         `json:"updated_by"`
}

func (q *Queries) CreateReferralSettings(ctx context.Context, arg CreateReferralSettingsParams) (ReferralSetting, error) {
	row := q.db.QueryRow(ctx, CreateReferralSettings,
		arg.ReferralRewardAmount,
		arg.CashbackPercentage,
		arg.MaxReferrals,
		arg.BetReferralBonusPercentage,
		arg.ExpiresAfterDays,
		arg.UpdatedBy,
	)
	var i ReferralSetting
	err := row.Scan(
		&i.ID,
		&i.ReferralRewardAmount,
		&i.CashbackPercentage,
		&i.BetReferralBonusPercentage,
		&i.MaxReferrals,
		&i.ExpiresAfterDays,
		&i.UpdatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Version,
	)
	return i, err
}

const GetReferralByCode = `-- name: GetReferralByCode :one
SELECT id, referral_code, referrer_id, referred_id, status, reward_amount, cashback_amount, created_at, updated_at, expires_at FROM referrals
WHERE referral_code = $1
`

func (q *Queries) GetReferralByCode(ctx context.Context, referralCode string) (Referral, error) {
	row := q.db.QueryRow(ctx, GetReferralByCode, referralCode)
	var i Referral
	err := row.Scan(
		&i.ID,
		&i.ReferralCode,
		&i.ReferrerID,
		&i.ReferredID,
		&i.Status,
		&i.RewardAmount,
		&i.CashbackAmount,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExpiresAt,
	)
	return i, err
}

const GetReferralByReferredID = `-- name: GetReferralByReferredID :one
SELECT id, referral_code, referrer_id, referred_id, status, reward_amount, cashback_amount, created_at, updated_at, expires_at FROM referrals WHERE referred_id = $1 LIMIT 1
`

func (q *Queries) GetReferralByReferredID(ctx context.Context, referredID pgtype.Text) (Referral, error) {
	row := q.db.QueryRow(ctx, GetReferralByReferredID, referredID)
	var i Referral
	err := row.Scan(
		&i.ID,
		&i.ReferralCode,
		&i.ReferrerID,
		&i.ReferredID,
		&i.Status,
		&i.RewardAmount,
		&i.CashbackAmount,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExpiresAt,
	)
	return i, err
}

const GetReferralSettings = `-- name: GetReferralSettings :one
SELECT id, referral_reward_amount, cashback_percentage, bet_referral_bonus_percentage, max_referrals, expires_after_days, updated_by, created_at, updated_at, version FROM referral_settings
WHERE id = 'default'
LIMIT 1
`

func (q *Queries) GetReferralSettings(ctx context.Context) (ReferralSetting, error) {
	row := q.db.QueryRow(ctx, GetReferralSettings)
	var i ReferralSetting
	err := row.Scan(
		&i.ID,
		&i.ReferralRewardAmount,
		&i.CashbackPercentage,
		&i.BetReferralBonusPercentage,
		&i.MaxReferrals,
		&i.ExpiresAfterDays,
		&i.UpdatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Version,
	)
	return i, err
}

const GetReferralStats = `-- name: GetReferralStats :one
SELECT
    COUNT(*) as total_referrals,
    COUNT(CASE WHEN status = 'COMPLETED' THEN 1 END) as completed_referrals,
    COALESCE(SUM(reward_amount), 0) as total_reward_earned,
    COALESCE(SUM(CASE WHEN status = 'PENDING' THEN reward_amount END), 0) as pending_rewards
FROM referrals
WHERE referrer_id = $1
`

type GetReferralStatsRow struct {
	TotalReferrals     int64       `json:"total_referrals"`
	CompletedReferrals int64       `json:"completed_referrals"`
	TotalRewardEarned  interface{} `json:"total_reward_earned"`
	PendingRewards     interface{} `json:"pending_rewards"`
}

func (q *Queries) GetReferralStats(ctx context.Context, referrerID string) (GetReferralStatsRow, error) {
	row := q.db.QueryRow(ctx, GetReferralStats, referrerID)
	var i GetReferralStatsRow
	err := row.Scan(
		&i.TotalReferrals,
		&i.CompletedReferrals,
		&i.TotalRewardEarned,
		&i.PendingRewards,
	)
	return i, err
}

const UpdateReferral = `-- name: UpdateReferral :one
UPDATE referrals
SET
    referred_id = $2,
    status = $3,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING id, referral_code, referrer_id, referred_id, status, reward_amount, cashback_amount, created_at, updated_at, expires_at
`

type UpdateReferralParams struct {
	ID         int64          `json:"id"`
	ReferredID pgtype.Text    `json:"referred_id"`
	Status     Referralstatus `json:"status"`
}

func (q *Queries) UpdateReferral(ctx context.Context, arg UpdateReferralParams) (Referral, error) {
	row := q.db.QueryRow(ctx, UpdateReferral, arg.ID, arg.ReferredID, arg.Status)
	var i Referral
	err := row.Scan(
		&i.ID,
		&i.ReferralCode,
		&i.ReferrerID,
		&i.ReferredID,
		&i.Status,
		&i.RewardAmount,
		&i.CashbackAmount,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ExpiresAt,
	)
	return i, err
}

const UpdateReferralCode = `-- name: UpdateReferralCode :exec
UPDATE users
SET
    referral_code = $2,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $1
`

type UpdateReferralCodeParams struct {
	ID           int64       `json:"id"`
	ReferralCode pgtype.Text `json:"referral_code"`
}

func (q *Queries) UpdateReferralCode(ctx context.Context, arg UpdateReferralCodeParams) error {
	_, err := q.db.Exec(ctx, UpdateReferralCode, arg.ID, arg.ReferralCode)
	return err
}

const UpdateReferralSettings = `-- name: UpdateReferralSettings :one
UPDATE referral_settings
SET
    referral_reward_amount = $2,
    cashback_percentage = $3,
    bet_referral_bonus_percentage= $4,
    max_referrals = $5,
    expires_after_days = $6,
    updated_by = $7,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING id, referral_reward_amount, cashback_percentage, bet_referral_bonus_percentage, max_referrals, expires_after_days, updated_by, created_at, updated_at, version
`

type UpdateReferralSettingsParams struct {
	ID                         int64          `json:"id"`
	ReferralRewardAmount       pgtype.Numeric `json:"referral_reward_amount"`
	CashbackPercentage         pgtype.Numeric `json:"cashback_percentage"`
	BetReferralBonusPercentage pgtype.Numeric `json:"bet_referral_bonus_percentage"`
	MaxReferrals               int32          `json:"max_referrals"`
	ExpiresAfterDays           int32          `json:"expires_after_days"`
	UpdatedBy                  string         `json:"updated_by"`
}

func (q *Queries) UpdateReferralSettings(ctx context.Context, arg UpdateReferralSettingsParams) (ReferralSetting, error) {
	row := q.db.QueryRow(ctx, UpdateReferralSettings,
		arg.ID,
		arg.ReferralRewardAmount,
		arg.CashbackPercentage,
		arg.BetReferralBonusPercentage,
		arg.MaxReferrals,
		arg.ExpiresAfterDays,
		arg.UpdatedBy,
	)
	var i ReferralSetting
	err := row.Scan(
		&i.ID,
		&i.ReferralRewardAmount,
		&i.CashbackPercentage,
		&i.BetReferralBonusPercentage,
		&i.MaxReferrals,
		&i.ExpiresAfterDays,
		&i.UpdatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Version,
	)
	return i, err
}
