/** Shared TypeScript interfaces for Mintall Keep admin UI. */

export interface CreditBalance {
	credits_remaining: number;
	credits_issued: number;
	/** True when the plan has no limit (mintall-backend#485). The counts
	 * above are placeholders in that case, so show "Unlimited" instead. */
	unlimited?: boolean;
	resets_at: string | null;
	subscription: {
		external_subscription_id: string | null;
		current_period_end_dt: string | null;
		status: string;
		price: {
			plan: string;
			plan_variant: string | null;
		};
	} | null;
}

export interface User {
	email: string;
}

export interface Account {
	user: User | null;
	/** Platform-account status, e.g. "active" | "disconnected" | "reauth_required". */
	status?: string;
	credit_balance: CreditBalance | null;
	/** A plan change scheduled to take effect in the future (e.g. a Woo
	 * downgrade-to-free that applies at the prepaid period end). */
	scheduled_plan: string | null;
	scheduled_plan_variant: string | null;
	scheduled_effective_at: string | null;
}

export interface AccountResponse {
	account: Account;
}

export interface ProductRow {
	product_id: number;
	product_name: string;
	product_image: string | null;
	product_url: string | null;
	status: ProtectionStatus;
	counts: ProtectionCounts | null;
}

/** Product state keyed by product_id (from GET /products/visibility). */
export interface ProductState {
	visible: boolean;
	readiness_score: number;
	primary_issue: string | null;
	/** Human-readable label for primary_issue, supplied by the backend. */
	primary_issue_label: string | null;
	/** Mintall internal product id, used to deep-link to the Keep web app. */
	mintall_product_id?: string | null;
}
export type ProductStateMap = Record<number, ProductState>;

/** MC4P certificate JSON stored per product (from GET /products/{id}/snapshot). */
export type ProductSnapshot = Record<string, unknown>;

export interface ProductsSyncResult {
	synced: number;
	errors: string[];
}

export interface ProductsResponse {
	items: ProductRow[];
	page: number;
	per_page: number;
	/** Whether a next page of synced products exists (backend list has no total). */
	has_more: boolean;
}

export type ProductStatusFilter = 'protected' | 'not-protected';
export type ProductVisibilityFilter = 'visible' | 'hidden';
export type ProductSortColumn = 'created_at' | 'readiness_score';
export type ProductSortOrder = 'asc' | 'desc';

export interface ProductsFilters {
	status: ProductStatusFilter | null;
	visibility: ProductVisibilityFilter | null;
	failingCheck: string | null;
	sortColumn: ProductSortColumn;
	sortOrder: ProductSortOrder;
}

export interface Price {
	plan: string;
	plan_variant: string;
	unit_amount: number | null;
}

export interface BillingResponse {
	prices: Price[];
}

export interface WooInitiateResponse {
	confirmation_url: string | null;
	contract_id: string | null;
	switched: boolean;
	plan: string;
	variant: string | null;
}

export interface WooRecordResponse {
	success: boolean;
	subscription: Record<string, unknown>;
}

export interface WooReconcileResponse {
	success: boolean;
	subscription: Record<string, unknown>;
}

export interface WooCancelResponse {
	success: boolean;
	subscription: Record<string, unknown>;
}

export interface SubscribeResult {
	success: boolean;
}

export type ProtectionStatus = 'UNPROTECTED' | 'IN_PROGRESS' | 'PROTECTED' | 'NEEDS_ATTENTION';

export interface ProtectionCounts {
	total: number;
	done: number;
	failed: number;
	cancelled: number;
	obsolete: number;
}

export interface ProtectStatus {
	status: ProtectionStatus;
	counts: ProtectionCounts | null;
}

/** Backend rollup of AUTO protection jobs (GET /protection/summary). */
export interface ProtectionSummary {
	tier: string;
	since: string | null;
	total_queued: number;
	total_done: number;
	total_failed: number;
	active_jobs: number;
}

export interface ProtectionSummaryResponse {
	auto_protect: boolean;
	since: string | null;
	summary: ProtectionSummary | null;
}

export interface PageViewCaptureFields {
	page_view_capture: boolean;
	page_view_capture_decided: boolean;
}

export interface SettingsData extends PageViewCaptureFields {
	connected: boolean;
	site_url: string;
	platform_id: string;
	created_at: string;
	account_email: string;
	auto_protect: boolean;
	visibility_on_protect: boolean;
	/** Connection needs re-authorization (backend can't sign callbacks for our pubkey). */
	reauth_required: boolean;
}

export interface SettingsUpdateResponse extends PageViewCaptureFields {
	success: boolean;
	auto_protect: boolean;
	visibility_on_protect: boolean;
}

export interface OAuthInitResponse {
	redirect_url: string;
}

export interface BulkEnqueueSkip {
	product_id: number;
	reason: 'not_found' | 'no_images';
}

export interface BulkEnqueueResponse {
	requested: number[];
	enqueued: number[];
	skipped: BulkEnqueueSkip[];
}

export interface BulkStatusEntry {
	status: ProtectionStatus;
	counts: ProtectionCounts | null;
}

export interface BulkStatusResponse {
	products: Record<number, BulkStatusEntry>;
}

export interface ProductsByIdsResponse {
	items: ProductRow[];
}

export interface SetVisibilityResponse {
	success: boolean;
	enabled: boolean;
	count: number;
	/** Bulk-enable only: ids that actually got enabled - may be fewer than
	 * requested if the plan's AI Visibility capacity ran out partway through. */
	enabled_ids?: number[];
	/** Bulk-enable only: how many requested ids were NOT enabled due to
	 * insufficient capacity. */
	failed_count?: number;
}

export interface PromoValidateResult {
	valid: boolean;
	discount_percent: number | null;
}

export interface CreditBalanceResponse {
	credit_balance: CreditBalance | null;
}

export interface SetupStatusSteps {
	protect_product: boolean;
	sync_products: boolean;
	enable_visibility: boolean;
	verify_domain: boolean;
}

export interface SetupStatusResponse extends PageViewCaptureFields {
	steps: SetupStatusSteps;
	dismissed: boolean;
	auto_protect: boolean;
	visibility_on_protect: boolean;
	domain_verification_status: DomainVerificationStatus | null;
	domain_verification_domain: string | null;
	current_site_host: string | null;
	host_is_verifiable: boolean;
}

export type DomainVerificationStatus = 'pending' | 'verified' | 'failed' | 'expired';

export interface DomainVerificationData {
	status: DomainVerificationStatus | null;
	domain: string | null;
	record_name: string;
	record_value: string | null;
	expires_at: string | null;
	verified_at: string | null;
	detail?: string | null;
	current_site_host: string | null;
	current_site_url: string;
	host_is_verifiable: boolean;
}
