// 상점 도메인 공유 타입 (Backend Response DTO 와 동기)

export type ProductType = 1|2;  // 1=Physical 실물, 2=Digital 쿠폰

export type SortKey = 'latest'|'sales'|'price_asc'|'price_desc';

export type OrderStatus = 1|2|3|4|5|6|7|8;
// 1=Pending, 2=Paid, 3=Preparing, 4=Shipped, 5=Delivered, 6=Cancelled, 7=Refunded, 8=Exchanged

export type ShipmentStatus = 1|2|3|4|5;
// 1=Preparing, 2=Pickup, 3=InTransit, 4=Delivered, 5=Failed

// 할인 모드: 0=None, 1=Percent, 2=Amount (Backend DiscountType enum 미러)
export type DiscountType = 0|1|2;

export interface ProductListRow {
	id: number;
	gameID: number;
	gameName: string;
	gamePublisher: string;
	name: string;
	thumbnail: string|null;
	type: ProductType;
	price: number;
	discountType: DiscountType;
	discountValue: number;
	salePrice: number;
	stock: number;
	saleStartAt: string|null;
	saleEndAt: string|null;
}

export interface ProductListResponse {
	total: number;
	list: ProductListRow[];
}

export interface GameOption {
	id: number;
	korName: string;
}

export interface GameListResponse {
	list: GameOption[];
}

export interface RelatedProductRow {
	id: number;
	name: string;
	thumbnail: string|null;
	type: ProductType;
	price: number;
	discountType: DiscountType;
	discountValue: number;
	salePrice: number;
}

export interface ProductDetail {
	id: number;
	gameID: number;
	gameKorName: string;
	gameEngName: string|null;
	gamePublisher: string;
	gameThumbnail: string|null;
	gameBigImage: string|null;
	gameDescription: string|null;
	gameLink: string|null;
	name: string;
	description: string|null;
	thumbnail: string|null;
	type: ProductType;
	price: number;
	discountType: DiscountType;
	discountValue: number;
	salePrice: number;
	stock: number;
	minPurchase: number;
	maxPurchase: number;
	saleStartAt: string|null;
	saleEndAt: string|null;
	requireDonationChannel: boolean;
	relatedProducts: RelatedProductRow[];
}

export type RefundType = 1|2|3|4;
// 1=Cancel, 2=Return, 3=Exchange, 4=Refund

export type RefundStatus = 1|2|3|4;
// 1=Requested, 2=Approved, 3=Rejected, 4=Completed

export type RefundReasonType = 1|2|3|4|5|6|7;
// 1=ChangedMind, 2=OptionChange, 3=ProductDefect, 4=ProductInfoMismatch, 5=DeliveryDelay, 6=WrongDelivery, 7=Other

export const REFUND_REASON_LABEL: Record<RefundReasonType, string> = {
	1: '단순 변심',
	2: '옵션/사이즈 변경',
	3: '상품 불량/파손',
	4: '상품 정보와 다름',
	5: '배송 지연/누락',
	6: '오배송',
	7: '기타'
};

export const REFUND_TYPE_LABEL: Record<RefundType, string> = {
	1: '주문 취소',
	2: '반품',
	3: '교환',
	4: '환불'
};

export const REFUND_STATUS_LABEL: Record<RefundStatus, string> = {
	1: '접수',
	2: '승인됨',
	3: '거부됨',
	4: '처리완료'
};

export interface OrderRefundRow {
	id: number;
	type: RefundType;
	reasonType: RefundReasonType;
	status: RefundStatus;
	amount: number;
	reason: string;
	adminMemo: string|null;
	requestedAt: string;
	resolvedAt: string|null;
}

export interface OrderListRow {
	id: number;
	orderNumber: string;
	totalAmount: number;
	status: OrderStatus;
	channelID: number|null;
	channelName: string|null;
	hasPhysical: boolean;
	hasDigital: boolean;
	createdAt: string;
	paidAt: string|null;
	itemCount: number;
	firstItemName: string;
	firstItemThumbnail: string|null;
	shipmentStatus: ShipmentStatus|null;
	hasActiveRefundRequest: boolean;
}

export interface OrderListResponse {
	total: number;
	list: OrderListRow[];
}

export interface OrderDetailItem {
	id: number;
	productID: number;
	productName: string;
	productThumbnail: string|null;
	type: ProductType;
	quantity: number;
	unitPrice: number;
	issuedCouponCodeID: number|null;
}

export interface OrderDetailShipment {
	id: number;
	status: ShipmentStatus;
	carrier: string|null;
	trackingNumber: string|null;
	shippingFee: number;
	shippedAt: string|null;
	deliveredAt: string|null;
}

export interface OrderDetail {
	id: number;
	orderNumber: string;
	totalAmount: number;
	platformFeeAmount: number;
	gameRevenueAmount: number;
	channelRewardAmount: number;
	status: OrderStatus;
	channelID: number|null;
	channelName: string|null;
	createdAt: string;
	paidAt: string|null;
	items: OrderDetailItem[];
	shipment: OrderDetailShipment|null;
	refunds: OrderRefundRow[];
}

export interface InventoryRow {
	id: number;
	orderItemID: number;
	couponCodeID: number;
	productID: number;
	productName: string;
	productThumbnail: string|null;
	couponName: string;
	gameName: string;
	acquiredAt: string;
	usedAt: string|null;
	expiresAt: string|null;
	isExpired: boolean;
	code: string|null;
}

export interface InventoryListResponse {
	total: number;
	list: InventoryRow[];
}

export interface InventoryGroupRow {
	productID: number;
	productName: string;
	productThumbnail: string|null;
	gameName: string;
	totalCount: number;
	unusedCount: number;
	earliestAcquiredAt: string;
}

export interface InventoryGroupsResponse {
	total: number;
	list: InventoryGroupRow[];
}

export interface UseCouponResponse {
	code: string;
	usedAt: string;
}

export interface PlaceOrderItem {
	productID: number;
	quantity: number;
}

export interface PlaceOrderRequest {
	channelID: number|null;
	items: PlaceOrderItem[];
}

export interface PlaceOrderResponse {
	orderID: number;
	orderNumber: string;
	totalAmount: number;
	platformFeeAmount: number;
	gameRevenueAmount: number;
	channelRewardAmount: number;
	hasPhysical: boolean;
	hasDigital: boolean;
}

export interface ChannelSearchRow {
	id: number;
	sid: string;
	name: string;
	handle: string|null;
	thumbnailUrl: string|null;
	subscriberCount: number;
	isVerified: boolean;
	donationCode: string|null;
}

export interface ChannelSearchResponse {
	total: number;
	list: ChannelSearchRow[];
}

export const ORDER_STATUS_LABEL: Record<OrderStatus, string> = {
	1: '결제 전',
	2: '결제완료',
	3: '준비중',
	4: '배송중',
	5: '배송완료',
	6: '취소',
	7: '환불',
	8: '교환'
};

export const SHIPMENT_STATUS_LABEL: Record<ShipmentStatus, string> = {
	1: '준비중',
	2: '집화',
	3: '배송중',
	4: '배송완료',
	5: '실패'
};

export const SORT_OPTIONS: { value: SortKey; label: string }[] = [
	{ value: 'latest', label: '최신순' },
	{ value: 'sales', label: '판매량순' },
	{ value: 'price_asc', label: '낮은 가격순' },
	{ value: 'price_desc', label: '높은 가격순' }
];
