// Admin-related types

export interface Banner {
  id: number;
  name: string;
  imageUrl: string;
  description: string | null;
  productIds: number[] | null;
  redirectUrl: string | null;
  serialNum: number | null;
  isActive: boolean;
  createdAt: Date;
  lastUpdated: Date;
}

export interface Complaint {
  id: number;
  complaintBody: string;
  userId: number;
  orderId: number | null;
  isResolved: boolean;
  response: string | null;
  createdAt: Date;
  images: string[] | null;
}

export interface ComplaintWithUser extends Complaint {
  userName: string | null;
  userMobile: string | null;
}

export interface Constant {
  key: string;
  value: any;
}

export interface ConstantUpdateResult {
  success: boolean;
  updatedCount: number;
  keys: string[];
}

export interface Coupon {
  id: number;
  couponCode: string;
  isUserBased: boolean;
  discountPercent: string | null;
  flatDiscount: string | null;
  minOrder: string | null;
  productIds: number[] | null;
  maxValue: string | null;
  isApplyForAll: boolean;
  validTill: Date | null;
  maxLimitForUser: number | null;
  exclusiveApply: boolean;
  isInvalidated: boolean;
  createdAt: Date;
  createdBy: number;
}

export interface CouponValidationResult {
  valid: boolean;
  message?: string;
  discountAmount?: number;
  coupon?: Partial<Coupon>;
}

export interface UserMiniInfo {
  id: number;
  name: string;
  mobile: string | null;
}

export interface Store {
  id: number;
  name: string;
  description: string | null;
  imageUrl: string | null;
  owner: number;
  createdAt: Date;
  // updatedAt: Date;
}

export interface StaffUser {
  id: number;
  name: string;
  password: string;
  staffRoleId: number;
  createdAt: Date;
}

export interface StaffRole {
  id: number;
  roleName: string;
}

export interface AdminOrderRow {
  id: number;
  userId: number;
  addressId: number;
  slotId: number | null;
  isCod: boolean;
  isOnlinePayment: boolean;
  paymentInfoId: number | null;
  totalAmount: string;
  deliveryCharge: string;
  readableId: number;
  adminNotes: string | null;
  userNotes: string | null;
  orderGroupId: string | null;
  orderGroupProportion: string | null;
  isFlashDelivery: boolean;
  createdAt: Date;
}

export type PaymentStatus = 'pending' | 'success' | 'cod' | 'failed'

export type RefundStatus = 'success' | 'pending' | 'failed' | 'none' | 'na' | 'processed'

export interface AdminOrderStatusRecord {
  id: number;
  orderTime: Date;
  userId: number;
  orderId: number;
  isPackaged: boolean;
  isDelivered: boolean;
  isCancelled: boolean;
  cancelReason: string | null;
  isCancelledByAdmin: boolean | null;
  paymentStatus: PaymentStatus;
  cancellationUserNotes: string | null;
  cancellationAdminNotes: string | null;
  cancellationReviewed: boolean;
  cancellationReviewedAt: Date | null;
  refundCouponId: number | null;
}

export interface AdminRefundRecord {
  id: number;
  orderId: number;
  refundAmount: string | null;
  refundStatus: RefundStatus | null;
  merchantRefundId: string | null;
  refundProcessedAt: Date | null;
  createdAt: Date;
}

export interface AdminOrderDetailsAddress {
  name: string;
  line1: string;
  line2: string | null;
  city: string;
  state: string;
  pincode: string;
  phone: string;
}

export interface AdminOrderDetailsSlotInfo {
  time: string;
  sequence: unknown;
}

export interface AdminOrderDetailsItem {
  id: number;
  name: string;
  quantity: string;
  productSize: number;
  price: string;
  unit?: string;
  amount: number;
  isPackaged: boolean;
  isPackageVerified: boolean;
}

export interface AdminOrderDetailsPayment {
  status: string;
  gateway: string;
  merchantOrderId: string;
}

export interface AdminOrderDetailsCouponData {
  couponCode: string;
  couponDescription: string;
  discountAmount: number;
}

export interface AdminOrderDetails {
  id: number;
  readableId: number;
  userId: number;
  customerName: string;
  customerEmail: string | null;
  customerMobile: string | null;
  address: AdminOrderDetailsAddress;
  slotInfo: AdminOrderDetailsSlotInfo | null;
  isCod: boolean;
  isOnlinePayment: boolean;
  totalAmount: number;
  deliveryCharge: number;
  adminNotes: string | null;
  userNotes: string | null;
  createdAt: Date;
  status: 'pending' | 'delivered' | 'cancelled';
  isPackaged: boolean;
  isDelivered: boolean;
  items: AdminOrderDetailsItem[];
  payment: AdminOrderDetailsPayment | null;
  paymentInfo: AdminOrderDetailsPayment | null;
  cancelReason: string | null;
  cancellationReviewed: boolean;
  isRefundDone: boolean;
  refundStatus: RefundStatus | null;
  refundAmount: number | null;
  couponData: AdminOrderDetailsCouponData | null;
  couponCode: string | null;
  couponDescription: string | null;
  discountAmount: number | null;
  orderStatus: AdminOrderStatusRecord | null;
  refundRecord: AdminRefundRecord | null;
  isFlashDelivery: boolean;
}

export interface AdminOrderUpdateResult {
  success: boolean;
  userId: number | null;
}

export interface AdminOrderItemPackagingResult {
  success: boolean;
  updated: boolean;
}

export interface AdminOrderMessageResult {
  success: boolean;
  message: string;
}

export interface AdminOrderBasicResult {
  success: boolean;
}

export interface AdminSlotOrderItem {
  id: number;
  name: string;
  quantity: number;
  price: number;
  amount: number;
  unit: string;
  isPackaged: boolean;
  isPackageVerified: boolean;
}

export interface AdminSlotOrder {
  id: number;
  readableId: number;
  customerName: string;
  address: string;
  addressId: number;
  latitude: number | null;
  longitude: number | null;
  totalAmount: number;
  items: AdminSlotOrderItem[];
  deliveryTime: string | null;
  status: 'pending' | 'delivered' | 'cancelled';
  isPackaged: boolean;
  isDelivered: boolean;
  isCod: boolean;
  paymentMode: 'COD' | 'Online';
  paymentStatus: PaymentStatus;
  slotId: number | null;
  adminNotes: string | null;
  userNotes: string | null;
}

export interface AdminGetSlotOrdersResult {
  success: boolean;
  data: AdminSlotOrder[];
}

export interface AdminOrderListItemProduct {
  id: number;
  name: string;
  quantity: number;
  price: number;
  amount: number;
  unit: string;
  productSize: number;
  isPackaged: boolean;
  isPackageVerified: boolean;
}

export interface AdminOrderListItem {
  id: number;
  orderId: string;
  readableId: number;
  customerName: string;
  customerMobile: string | null;
  address: string;
  addressId: number;
  latitude: number | null;
  longitude: number | null;
  totalAmount: number;
  deliveryCharge: number;
  items: AdminOrderListItemProduct[];
  createdAt: Date;
  deliveryTime: string | null;
  status: 'pending' | 'delivered' | 'cancelled';
  isPackaged: boolean;
  isDelivered: boolean;
  isCod: boolean;
  isFlashDelivery: boolean;
  userNotes: string | null;
  adminNotes: string | null;
  userNegativityScore: number;
}

export interface AdminOrderListItemWithUserId extends AdminOrderListItem {
  userId: number;
}

export interface AdminGetAllOrdersResult {
  orders: AdminOrderListItem[];
  nextCursor?: number;
}

export interface AdminGetAllOrdersResultWithUserId {
  orders: AdminOrderListItemWithUserId[];
  nextCursor?: number;
}

export interface AdminRebalanceSlotsResult {
  success: boolean;
  updatedOrders: number[];
  message: string;
}

export type AdminCancelOrderError =
  | 'order_not_found'
  | 'status_not_found'
  | 'already_cancelled'
  | 'already_delivered'

export interface AdminCancelOrderResult {
  success: boolean;
  message: string;
  orderId?: number;
  userId?: number;
  error?: AdminCancelOrderError;
}

export interface AdminUnit {
  id: number;
  shortNotation: string;
  fullName: string;
}

export interface AdminProduct {
  id: number;
  name: string;
  shortDescription: string | null;
  longDescription: string | null;
  unitId: number;
  price: string;
  marketPrice: string | null;
  images: string[] | null;
  imageKeys: string[] | null;
  isOutOfStock: boolean;
  isSuspended: boolean;
  isFlashAvailable: boolean;
  flashPrice: string | null;
  createdAt: Date;
  incrementStep: number;
  productQuantity: number;
  storeId: number | null;
}

export interface AdminProductWithRelations extends AdminProduct {
  unit: AdminUnit;
  store: Store | null;
}

export interface AdminProductTagInfo {
  id: number;
  tagName: string;
  tagDescription: string | null;
  imageUrl: string | null;
  isDashboardTag: boolean;
  relatedStores: unknown;
  createdAt: Date;
}

export interface AdminProductTagAssignment {
  productId: number;
  tagId: number;
  assignedAt: Date;
  product: AdminProduct;
}

export interface AdminProductTagWithProducts extends AdminProductTagInfo {
  products: AdminProductTagAssignment[];
}

export interface AdminSpecialDeal {
  id: number;
  productId: number;
  quantity: string;
  price: string;
  validTill: Date;
}

export interface AdminProductWithDetails extends AdminProduct {
  unit: AdminUnit;
  deals: AdminSpecialDeal[];
  tags: AdminProductTagInfo[];
}

export interface AdminProductListResponse {
  products: AdminProductWithRelations[];
  count: number;
}

export interface AdminProductResponse {
  product: AdminProductWithDetails;
}

export interface AdminDeleteProductResult {
  message: string;
}

export interface AdminToggleOutOfStockResult {
  product: AdminProduct;
  message: string;
}

export interface AdminUpdateSlotProductsResult {
  message: string;
  added: number;
  removed: number;
}

export interface AdminSlotProductIdsResult {
  productIds: number[];
}

export type AdminSlotsProductIdsResult = Record<number, number[]>;

export interface AdminProductReview {
  id: number;
  reviewBody: string;
  ratings: number;
  imageUrls: unknown;
  reviewTime: Date;
  adminResponse: string | null;
  adminResponseImages: unknown;
  userName: string | null;
}

export interface AdminProductReviewWithSignedUrls extends AdminProductReview {
  signedImageUrls: string[];
  signedAdminImageUrls: string[];
}

export interface AdminProductReviewsResult {
  reviews: AdminProductReviewWithSignedUrls[];
  hasMore: boolean;
}

export interface AdminProductReviewResponse {
  success: boolean;
  review: AdminProductReview;
}

export interface AdminProductGroup {
  id: number;
  groupName: string;
  description: string | null;
  createdAt: Date;
  products: AdminProduct[];
  productCount: number;
}

export interface AdminProductGroupsResult {
  groups: AdminProductGroup[];
}

export interface AdminProductGroupResponse {
  group: AdminProductGroupInfo;
  message: string;
}

export interface AdminProductGroupInfo {
  id: number;
  groupName: string;
  description: string | null;
  createdAt: Date;
}

export interface AdminUpdateProductPricesResult {
  message: string;
  updatedCount: number;
}

export interface AdminDeliverySlot {
  id: number;
  deliveryTime: Date;
  freezeTime: Date;
  isActive: boolean;
  isFlash: boolean;
  isCapacityFull: boolean;
  deliverySequence: unknown;
  groupIds: unknown;
}

export interface AdminSlotProductSummary {
  id: number;
  name: string;
  images: string[] | null;
}

export interface AdminVendorSnippet {
  id: number;
  snippetCode: string;
  slotId: number | null;
  productIds: number[];
  isPermanent: boolean;
  validTill: Date | null;
  createdAt: Date;
}

export interface AdminVendorSnippetWithAccess extends AdminVendorSnippet {
  accessUrl: string;
}

export interface AdminVendorSnippetWithSlot extends AdminVendorSnippet {
  slot: AdminDeliverySlot | null;
}

export interface AdminVendorSnippetProduct {
  id: number;
  name: string;
}

export interface AdminVendorSnippetWithProducts extends AdminVendorSnippetWithSlot {
  accessUrl: string;
  products: AdminVendorSnippetProduct[];
}

export interface AdminSlotWithProducts extends AdminDeliverySlot {
  deliverySequence: number[];
  products: AdminSlotProductSummary[];
}

export interface AdminSlotWithProductsAndSnippets extends AdminSlotWithProducts {
  groupIds: number[];
  vendorSnippets: AdminVendorSnippetWithAccess[];
}

export interface AdminSlotWithProductsAndSnippetsBase extends AdminSlotWithProducts {
  groupIds: number[];
  vendorSnippets: AdminVendorSnippet[];
}

export interface AdminSlotsResult {
  slots: AdminSlotWithProducts[];
  count: number;
}

export interface AdminSlotsListResult {
  slots: AdminDeliverySlot[];
  count: number;
}

export interface AdminSlotResult {
  slot: AdminSlotWithProductsAndSnippets;
}

export interface AdminSlotCreateResult {
  slot: AdminDeliverySlot;
  createdSnippets: AdminVendorSnippet[];
  message: string;
}

export interface AdminSlotUpdateResult {
  slot: AdminDeliverySlot;
  createdSnippets: AdminVendorSnippet[];
  message: string;
}

export interface AdminSlotDeleteResult {
  message: string;
}

export type AdminDeliverySequence = Record<string, number[]>;

export interface AdminDeliverySequenceResult {
  deliverySequence: AdminDeliverySequence;
}

export interface AdminUpdateDeliverySequenceResult {
  slot: {
    id: number;
    deliverySequence: unknown;
  };
  message: string;
}

export interface AdminUpdateSlotCapacityResult {
  success: boolean;
  slot: AdminDeliverySlot;
  message: string;
}

export interface AdminVendorSnippetCreateInput {
  snippetCode: string;
  slotId?: number;
  productIds: number[];
  validTill?: string;
  isPermanent: boolean;
}

export interface AdminVendorSnippetUpdateInput {
  snippetCode?: string;
  slotId?: number;
  productIds?: number[];
  validTill?: string | null;
  isPermanent?: boolean;
}

export interface AdminVendorSnippetDeleteResult {
  message: string;
}

export interface AdminVendorSnippetOrderProduct {
  orderItemId: number;
  productId: number;
  productName: string;
  quantity: number;
  productSize: number;
  price: number;
  unit: string;
  subtotal: number;
  is_packaged: boolean;
  is_package_verified: boolean;
}

export interface AdminVendorSnippetOrderSummary {
  orderId: string;
  orderDate: string;
  customerName: string;
  totalAmount: number;
  slotInfo: {
    time: string;
    sequence: unknown;
  } | null;
  products: AdminVendorSnippetOrderProduct[];
  matchedProducts: number[];
  snippetCode: string;
}

export interface AdminVendorSnippetOrdersResult {
  success: boolean;
  data: AdminVendorSnippetOrderSummary[];
  snippet: {
    id: number;
    snippetCode: string;
    slotId: number | null;
    productIds: number[];
    validTill?: string;
    createdAt: string;
    isPermanent: boolean;
  };
}

export interface AdminVendorSnippetOrdersWithSlotResult extends AdminVendorSnippetOrdersResult {
  selectedSlot: {
    id: number;
    deliveryTime: string;
    freezeTime: string;
    deliverySequence: unknown;
  };
}

export interface AdminVendorOrderSummary {
  id: number;
  status: string;
  orderDate: string;
  totalQuantity: number;
  products: {
    name: string;
    quantity: number;
    unit: string;
  }[];
}

export interface AdminUpcomingSlotsResult {
  success: boolean;
  data: {
    id: number;
    deliveryTime: string;
    freezeTime: string;
    deliverySequence: unknown;
  }[];
}

export type AdminVendorUpdatePackagingResult =
  | {
      success: true;
      orderItemId: number;
      is_packaged: boolean;
    }
  | {
      success: false;
      message: string;
    }
