// ==============================================================================
// API .NET + Next.js - Frontend Contracts (TypeScript)
// Types compartilhados entre API e Frontend
// ==============================================================================

// ==============================================================================
// PAGINATION
// ==============================================================================

export interface PaginationParams {
  page?: number;
  pageSize?: number;
  sortBy?: string;
  sortDesc?: boolean;
  search?: string;
}

export interface PagedResult<T> {
  items: T[];
  totalItems: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

// ==============================================================================
// ENTITIES
// ==============================================================================

export interface Product {
  id: number;
  name: string;
  description?: string;
  price: number;
  stock: number;
  category?: string;
  imageUrl?: string;
  isActive: boolean;
  createdAt: string;
  updatedAt?: string;
}

export interface Order {
  id: number;
  orderNumber: string;
  customerId: number;
  customer?: Customer;
  status: OrderStatus;
  items: OrderItem[];
  subtotal: number;
  tax: number;
  total: number;
  notes?: string;
  createdAt: string;
  updatedAt?: string;
}

export interface OrderItem {
  id: number;
  productId: number;
  product?: Product;
  quantity: number;
  unitPrice: number;
  total: number;
}

export interface Customer {
  id: number;
  name: string;
  email: string;
  phone?: string;
  address?: Address;
  createdAt: string;
}

export interface Address {
  street: string;
  number: string;
  complement?: string;
  neighborhood: string;
  city: string;
  state: string;
  zipCode: string;
  country: string;
}

export interface User {
  id: string;
  email: string;
  name?: string;
  imageUrl?: string;
  role: UserRole;
  createdAt: string;
  lastLoginAt?: string;
}

// ==============================================================================
// ENUMS
// ==============================================================================

export type OrderStatus =
  | "Pending"
  | "Confirmed"
  | "Processing"
  | "Shipped"
  | "Delivered"
  | "Cancelled";

export type UserRole = "Admin" | "Manager" | "User" | "Viewer";

export type ProductCategory =
  | "Electronics"
  | "Clothing"
  | "Food"
  | "Books"
  | "Other";

// ==============================================================================
// REQUEST DTOs
// ==============================================================================

export interface CreateProductRequest {
  name: string;
  description?: string;
  price: number;
  stock: number;
  category?: string;
  imageUrl?: string;
}

export interface UpdateProductRequest {
  name?: string;
  description?: string;
  price?: number;
  stock?: number;
  category?: string;
  imageUrl?: string;
  isActive?: boolean;
}

export interface CreateOrderRequest {
  customerId: number;
  items: CreateOrderItemRequest[];
  notes?: string;
}

export interface CreateOrderItemRequest {
  productId: number;
  quantity: number;
}

export interface UpdateOrderStatusRequest {
  status: OrderStatus;
  notes?: string;
}

export interface CreateCustomerRequest {
  name: string;
  email: string;
  phone?: string;
  address?: Address;
}

// ==============================================================================
// RESPONSE DTOs
// ==============================================================================

export interface ApiResponse<T> {
  data: T;
  success: boolean;
  message?: string;
}

export interface ApiError {
  type: string;
  title: string;
  status: number;
  detail?: string;
  instance?: string;
  errors?: Record<string, string[]>;
}

export interface DashboardStats {
  totalProducts: number;
  totalOrders: number;
  totalCustomers: number;
  totalRevenue: number;
  ordersToday: number;
  revenueToday: number;
  recentOrders: Order[];
  topProducts: ProductSales[];
}

export interface ProductSales {
  product: Product;
  totalSold: number;
  totalRevenue: number;
}

// ==============================================================================
// FILTER DTOs
// ==============================================================================

export interface ProductFilters extends PaginationParams {
  category?: string;
  minPrice?: number;
  maxPrice?: number;
  inStock?: boolean;
  isActive?: boolean;
}

export interface OrderFilters extends PaginationParams {
  status?: OrderStatus;
  customerId?: number;
  startDate?: string;
  endDate?: string;
  minTotal?: number;
  maxTotal?: number;
}

export interface CustomerFilters extends PaginationParams {
  city?: string;
  state?: string;
}

// ==============================================================================
// FORM SCHEMAS (Zod)
// ==============================================================================

// Exemplo de uso com Zod:
/*
import { z } from "zod";

export const createProductSchema = z.object({
  name: z.string().min(1, "Name is required").max(100),
  description: z.string().max(500).optional(),
  price: z.number().positive("Price must be positive"),
  stock: z.number().int().nonnegative("Stock cannot be negative"),
  category: z.string().optional(),
  imageUrl: z.string().url().optional(),
});

export type CreateProductFormData = z.infer<typeof createProductSchema>;

export const createOrderSchema = z.object({
  customerId: z.number().int().positive(),
  items: z.array(z.object({
    productId: z.number().int().positive(),
    quantity: z.number().int().positive("Quantity must be at least 1"),
  })).min(1, "At least one item is required"),
  notes: z.string().max(500).optional(),
});

export type CreateOrderFormData = z.infer<typeof createOrderSchema>;
*/

// ==============================================================================
// REACT QUERY KEYS
// ==============================================================================

export const queryKeys = {
  products: {
    all: ["products"] as const,
    list: (filters: ProductFilters) => ["products", "list", filters] as const,
    detail: (id: number) => ["products", "detail", id] as const,
  },
  orders: {
    all: ["orders"] as const,
    list: (filters: OrderFilters) => ["orders", "list", filters] as const,
    detail: (id: number) => ["orders", "detail", id] as const,
  },
  customers: {
    all: ["customers"] as const,
    list: (filters: CustomerFilters) =>
      ["customers", "list", filters] as const,
    detail: (id: number) => ["customers", "detail", id] as const,
  },
  dashboard: ["dashboard"] as const,
};

// ==============================================================================
// API ENDPOINTS
// ==============================================================================

export const apiEndpoints = {
  products: {
    list: "/api/products",
    detail: (id: number) => `/api/products/${id}`,
    create: "/api/products",
    update: (id: number) => `/api/products/${id}`,
    delete: (id: number) => `/api/products/${id}`,
  },
  orders: {
    list: "/api/orders",
    detail: (id: number) => `/api/orders/${id}`,
    create: "/api/orders",
    updateStatus: (id: number) => `/api/orders/${id}/status`,
    cancel: (id: number) => `/api/orders/${id}/cancel`,
  },
  customers: {
    list: "/api/customers",
    detail: (id: number) => `/api/customers/${id}`,
    create: "/api/customers",
    update: (id: number) => `/api/customers/${id}`,
  },
  dashboard: "/api/dashboard",
};
