package lu.rewardflow.terminal.data.model

import com.squareup.moshi.JsonClass

// ── Auth ────────────────────────────────────────────────────────────────

@JsonClass(generateAdapter = true)
data class LoginRequest(
    val email_or_username: String,
    val password: String,
)

@JsonClass(generateAdapter = true)
data class LoginResponse(
    val access_token: String,
    val token_type: String,
    val expires_in: Int,
)

// ── Program ─────────────────────────────────────────────────────────────

@JsonClass(generateAdapter = true)
data class ProgramResponse(
    val id: Int,
    val merchant_id: Int,
    val loyalty_type: String,
    val display_name: String,
    val card_name: String,
    val card_color: String,
    val card_secondary_color: String? = null,
    val logo_url: String? = null,
    val stamps_target: Int = 10,
    val points_per_euro: Int = 10,
    val welcome_bonus_points: Int = 0,
    val stamps_reward_description: String? = null,
    val require_staff_pin: Boolean = false,
    val is_active: Boolean = true,
)

// ── Card ────────────────────────────────────────────────────────────────

@JsonClass(generateAdapter = true)
data class CardResponse(
    val id: Int,
    val card_number: String,
    val customer_id: Int,
    val merchant_id: Int,
    val points_balance: Int = 0,
    val stamp_count: Int = 0,
    val is_active: Boolean = true,
)

@JsonClass(generateAdapter = true)
data class CardLookupResponse(
    val id: Int,
    val card_number: String,
    val customer_id: Int,
    val customer_name: String? = null,
    val customer_email: String = "",
    val merchant_id: Int,
    val stamp_count: Int = 0,
    val stamps_target: Int = 10,
    val stamps_until_reward: Int = 10,
    val points_balance: Int = 0,
    val can_redeem_stamps: Boolean = false,
    val stamp_reward_description: String? = null,
    val available_rewards: List<RewardItem> = emptyList(),
    val can_stamp: Boolean = true,
    val stamps_today: Int = 0,
    val max_daily_stamps: Int = 5,
)

@JsonClass(generateAdapter = true)
data class CardDetailResponse(
    val id: Int,
    val card_number: String,
    val customer_name: String? = null,
    val customer_email: String? = null,
    val points_balance: Int = 0,
    val stamp_count: Int = 0,
    val stamps_target: Int = 10,
    val total_points_earned: Int = 0,
    val total_stamps_earned: Int = 0,
    val is_active: Boolean = true,
)

@JsonClass(generateAdapter = true)
data class RewardItem(
    val id: String,
    val name: String,
    val points_required: Int,
    val is_active: Boolean = true,
)

// ── Enrollment ──────────────────────────────────────────────────────────

@JsonClass(generateAdapter = true)
data class EnrollRequest(
    val customer_id: Int? = null,
    val email: String? = null,
    val customer_name: String? = null,
    val customer_phone: String? = null,
    val customer_birthday: String? = null,
)

// ── Stamps ──────────────────────────────────────────────────────────────

@JsonClass(generateAdapter = true)
data class StampRequest(
    val card_id: Int? = null,
    val qr_code: String? = null,
    val card_number: String? = null,
    val staff_pin: String? = null,
    val category_ids: List<Int>? = null,
    val notes: String? = null,
)

@JsonClass(generateAdapter = true)
data class StampResponse(
    val success: Boolean,
    val stamp_count: Int = 0,
    val stamps_target: Int = 10,
    val reward_earned: Boolean = false,
    val reward_description: String? = null,
)

@JsonClass(generateAdapter = true)
data class StampRedeemRequest(
    val card_id: Int? = null,
    val qr_code: String? = null,
    val card_number: String? = null,
    val staff_pin: String? = null,
)

@JsonClass(generateAdapter = true)
data class StampRedeemResponse(
    val success: Boolean,
    val stamp_count: Int = 0,
)

// ── Points ──────────────────────────────────────────────────────────────

@JsonClass(generateAdapter = true)
data class PointsEarnRequest(
    val card_id: Int? = null,
    val qr_code: String? = null,
    val card_number: String? = null,
    val purchase_amount_cents: Int,
    val order_reference: String? = null,
    val staff_pin: String? = null,
    val category_ids: List<Int>? = null,
    val notes: String? = null,
)

@JsonClass(generateAdapter = true)
data class PointsEarnResponse(
    val success: Boolean,
    val points_earned: Int = 0,
    val points_balance: Int = 0,
)

@JsonClass(generateAdapter = true)
data class PointsRedeemRequest(
    val card_id: Int? = null,
    val qr_code: String? = null,
    val card_number: String? = null,
    val reward_id: String,
    val staff_pin: String? = null,
)

@JsonClass(generateAdapter = true)
data class PointsRedeemResponse(
    val success: Boolean,
    val points_balance: Int = 0,
)

// ── PINs ────────────────────────────────────────────────────────────────

@JsonClass(generateAdapter = true)
data class PinListResponse(
    val pins: List<PinItem>,
    val total: Int,
)

@JsonClass(generateAdapter = true)
data class PinItem(
    val id: Int,
    val name: String,
    val is_active: Boolean,
    val is_locked: Boolean,
)

@JsonClass(generateAdapter = true)
data class PinForDeviceListResponse(
    val pins: List<PinForDeviceItem>,
    val total: Int,
)

@JsonClass(generateAdapter = true)
data class PinForDeviceItem(
    val id: Int,
    val name: String,
    val staff_id: String? = null,
    val is_active: Boolean = true,
    val is_locked: Boolean = false,
    /** bcrypt hash of the PIN — only ever returned by /pins/for-device,
     *  consumed by [PinVerifier]. */
    val pin_hash: String,
)

// ── Pairing ─────────────────────────────────────────────────────────────

/**
 * JSON payload encoded in the pairing QR generated by
 * ``POST /api/v1/merchants/loyalty/devices`` on the backend.
 *
 * The tablet decodes this once at the setup screen, persists the three
 * fields via DeviceConfigRepository, and then never sees the QR again.
 */
@JsonClass(generateAdapter = true)
data class SetupPayload(
    val api_url: String,
    val store_id: Int,
    val store_code: String,
    val store_name: String? = null,
    val auth_token: String,
)

// ── Transactions ────────────────────────────────────────────────────────

@JsonClass(generateAdapter = true)
data class TransactionListResponse(
    val transactions: List<TransactionItem>,
    val total: Int,
)

@JsonClass(generateAdapter = true)
data class TransactionItem(
    val id: Int,
    val card_id: Int,
    val transaction_type: String,
    val stamps_delta: Int = 0,
    val points_delta: Int = 0,
    val customer_name: String? = null,
    val staff_name: String? = null,
    val purchase_amount_cents: Int? = null,
    val reward_description: String? = null,
    val transaction_at: String,
)

// ── Categories ──────────────────────────────────────────────────────────

@JsonClass(generateAdapter = true)
data class CategoryListResponse(
    val categories: List<CategoryItem>,
    val total: Int,
)

@JsonClass(generateAdapter = true)
data class CategoryItem(
    val id: Int,
    val store_id: Int,
    val name: String,
    /** Per-language overrides keyed by language code (en/fr/de/lb). Null = use `name`. */
    val name_translations: Map<String, String>? = null,
    val display_order: Int = 0,
    val is_active: Boolean = true,
)
