import type {
  UserRole,
  LicenseType,
  TrainingStatus,
  FunnelStage,
  SaleStatus,
  WebhookEvent,
  NotificationType,
} from '@fitcrm/shared-types';

// --- Common ---

export interface PaginationParams {
  cursor?: string;
  limit?: number;
}

export interface PaginatedResponse<T> {
  data: T[];
  nextCursor: string | null;
  hasMore: boolean;
  total?: number;
}

// --- Auth ---

export interface LoginRequest {
  phone: string;
  password: string;
}

export interface LoginResponse {
  accessToken: string;
  refreshToken: string;
  user: {
    id: string;
    firstName: string;
    lastName: string;
    role: UserRole;
    clubId: string;
  };
}

export interface RefreshRequest {
  refreshToken: string;
}

export interface RefreshResponse {
  accessToken: string;
  refreshToken?: string;
}

// --- Users ---

export interface User {
  id: string;
  clubId: string;
  firstName: string;
  lastName: string;
  phone: string;
  email: string | null;
  role: UserRole;
  isActive: boolean;
  departmentId: string | null;
  createdAt: string;
}

// --- Clients ---

export interface Client {
  id: string;
  clubId: string;
  firstName: string;
  lastName: string;
  phone: string;
  email: string | null;
  birthDate: string | null;
  gender: string | null;
  notes: string | null;
  trainerId: string | null;
  createdAt: string;
}

// --- Departments ---

export interface Department {
  id: string;
  clubId: string;
  name: string;
  description: string | null;
  createdAt: string;
  _count?: { staff: number };
}

export interface CreateDepartmentRequest {
  name: string;
  description?: string;
}

export interface UpdateDepartmentRequest {
  name?: string;
  description?: string;
}

// --- Rooms ---

export interface Room {
  id: string;
  clubId: string;
  name: string;
  capacity: number | null;
  description: string | null;
  isActive: boolean;
  createdAt: string;
}

export interface CreateRoomRequest {
  name: string;
  capacity?: number;
  description?: string;
  isActive?: boolean;
}

export interface UpdateRoomRequest {
  name?: string;
  capacity?: number;
  description?: string;
  isActive?: boolean;
}

// --- Catalog ---

export interface ServiceCategory {
  id: string;
  clubId: string;
  name: string;
  description: string | null;
  sortOrder: number;
  createdAt: string;
}

export interface Service {
  id: string;
  clubId: string;
  categoryId: string;
  name: string;
  description: string | null;
  duration: number;
  price: number;
  isActive: boolean;
  createdAt: string;
  category?: ServiceCategory;
}

export interface Package {
  id: string;
  clubId: string;
  serviceId: string;
  name: string;
  sessionsCount: number;
  price: number;
  validDays: number;
  isActive: boolean;
  createdAt: string;
  service?: Service;
}

// --- Clubs (admin) ---

export interface Club {
  id: string;
  name: string;
  slug: string;
  address: string | null;
  phone: string | null;
  email: string | null;
  logoUrl: string | null;
  timezone: string | null;
  isActive: boolean;
  createdAt: string;
  license?: License;
}

export interface CreateClubRequest {
  name: string;
  slug: string;
  address?: string;
  phone?: string;
  email?: string;
}

export interface UpdateClubRequest {
  name?: string;
  address?: string;
  phone?: string;
  email?: string;
  logoUrl?: string;
  timezone?: string;
  isActive?: boolean;
}

// --- Licenses ---

export interface License {
  id: string;
  clubId: string;
  type: LicenseType;
  startDate: string;
  endDate: string;
  isActive: boolean;
  gracePeriodEnd: string | null;
  maxUsers: number;
  maxClients: number;
  createdAt: string;
  status?: string;
  daysUntilExpiry?: number;
}

export interface CreateLicenseRequest {
  clubId: string;
  type: LicenseType;
  maxUsers: number;
  maxClients: number;
  startDate?: string;
  endDate?: string;
}

export interface UpdateLicenseRequest {
  type?: LicenseType;
  maxUsers?: number;
  maxClients?: number;
  endDate?: string;
  isActive?: boolean;
}

// --- Reports ---

export interface Report {
  id: string;
  clubId: string;
  generatedById: string;
  type: string;
  title: string;
  parametersJson: Record<string, unknown> | null;
  fileUrl: string | null;
  status: 'PENDING' | 'GENERATING' | 'COMPLETED' | 'FAILED';
  createdAt: string;
}

export interface GenerateReportRequest {
  type: string;
  title: string;
  parameters?: Record<string, unknown>;
}

// --- Webhooks ---

export interface WebhookSubscription {
  id: string;
  clubId: string;
  url: string;
  events: WebhookEvent[];
  isActive: boolean;
  secret?: string;
  createdAt: string;
}

export interface WebhookLog {
  id: string;
  clubId: string;
  subscriptionId: string;
  event: string;
  payload: unknown;
  statusCode: number | null;
  response: string | null;
  attempt: number;
  deliveredAt: string | null;
  createdAt: string;
}

export interface CreateWebhookRequest {
  url: string;
  events: WebhookEvent[];
}

export interface UpdateWebhookRequest {
  url?: string;
  events?: WebhookEvent[];
  isActive?: boolean;
}

// --- Integration ---

export interface IntegrationConfig {
  id: string;
  clubId: string;
  provider: string;
  apiUrl: string | null;
  apiKey: string | null;
  syncEnabled: boolean;
  syncIntervalMinutes: number;
  lastSyncAt: string | null;
  createdAt: string;
}

export interface UpdateIntegrationConfigRequest {
  apiUrl?: string;
  apiKey?: string;
  syncEnabled?: boolean;
  syncIntervalMinutes?: number;
}

export interface SyncLog {
  id: string;
  clubId: string;
  direction: string;
  entity: string;
  status: string;
  recordsProcessed: number;
  recordsFailed: number;
  errorMessage: string | null;
  startedAt: string;
  completedAt: string | null;
  createdAt: string;
}

export interface TriggerSyncRequest {
  direction: 'import' | 'export';
  entity: string;
}

// --- Schedule ---

export interface Training {
  id: string;
  clubId: string;
  trainerId: string;
  clientId: string;
  roomId: string | null;
  serviceId: string | null;
  startTime: string;
  endTime: string;
  status: TrainingStatus;
  notes: string | null;
  createdAt: string;
  trainer?: User;
  client?: Client;
}

// --- Funnel ---

export interface FunnelEntry {
  id: string;
  clubId: string;
  clientId: string;
  trainerId: string | null;
  stage: FunnelStage;
  source: string | null;
  comment: string | null;
  createdAt: string;
  client?: Client;
  trainer?: User;
}

// --- Sales ---

export interface Sale {
  id: string;
  clubId: string;
  clientId: string;
  trainerId: string;
  packageId: string | null;
  amount: number;
  status: SaleStatus;
  createdAt: string;
  client?: Client;
}

// --- Stats ---

export interface StatsSummary {
  activeClients: number;
  newClients: number;
  trainingsToday: number;
  revenue: number;
  conversionRate: number;
  sleepingClients: number;
}

export interface FunnelStats {
  stage: FunnelStage;
  count: number;
  conversion: number;
}

export interface RatingEntry {
  trainerId: string;
  trainerName: string;
  score: number;
  newClients: number;
  conversions: number;
  totalTrainings: number;
}

// --- Notifications ---

export interface Notification {
  id: string;
  clubId: string;
  userId: string;
  type: NotificationType;
  title: string;
  body: string;
  data: unknown;
  isRead: boolean;
  sentAt: string;
  createdAt: string;
}

export interface NotificationSetting {
  id: string;
  clubId: string;
  notificationType: NotificationType;
  enabled: boolean;
  customText: string | null;
}

// --- SIP ---

export interface SipCall {
  id: string;
  clubId: string;
  callerId: string;
  clientId: string | null;
  callerNumber: string;
  calleeNumber: string;
  direction: string;
  status: string;
  startedAt: string;
  duration: number | null;
  createdAt: string;
}

export interface SipSettings {
  domain: string;
  username: string;
  wsUrl: string;
  stunServer: string;
  turnServer: string;
}

// --- Metering ---

export interface UsageRecord {
  id: string;
  clubId: string;
  period: string;
  activeUsers: number;
  clients: number;
  callStorageGb: number;
  webhooksSent: number;
  pushSent: number;
  apiRequests: number;
}

export interface LimitCheck {
  resource: string;
  current: number;
  limit: number;
  exceeded: boolean;
}

// --- Club Modules ---

export interface ClubModule {
  id: string;
  clubId: string;
  moduleId: string;
  enabled: boolean;
  limitsJson: Record<string, unknown> | null;
  usageJson: Record<string, unknown> | null;
}
