import apiClient from './apiClient';

export interface GuestPreference {
  preferred_room_location?: string;
  preferred_floor?: number;
  preferred_room_type_id?: number;
  preferred_amenities?: string[];
  special_requests?: string;
  preferred_services?: string[];
  preferred_contact_method?: string;
  preferred_language?: string;
  dietary_restrictions?: string[];
  additional_preferences?: Record<string, any>;
}

export interface GuestNote {
  id: number;
  note: string;
  is_important: boolean;
  is_private: boolean;
  created_by?: string;
  created_at: string;
}

export interface GuestTag {
  id: number;
  name: string;
  color: string;
  description?: string;
}

export interface GuestSegment {
  id: number;
  name: string;
  description?: string;
  criteria?: Record<string, any>;
}

export interface GuestCommunication {
  id: number;
  communication_type: 'email' | 'phone' | 'sms' | 'chat' | 'in_person' | 'other';
  direction: 'inbound' | 'outbound';
  subject?: string;
  content: string;
  staff_name?: string;
  created_at: string;
}

export interface GuestBooking {
  id: number;
  booking_number: string;
  check_in_date: string;
  check_out_date: string;
  status: string;
  total_price: number;
}

export interface GuestAnalytics {
  lifetime_value: number;
  satisfaction_score?: number;
  booking_statistics: {
    total_bookings: number;
    completed_bookings: number;
    cancelled_bookings: number;
    last_visit_date?: string;
    total_nights_stayed: number;
  };
  preferred_room_type?: string;
  average_booking_value: number;
  communication_count: number;
  is_vip: boolean;
  total_visits: number;
  last_visit_date?: string;
}

export interface GuestProfile {
  id: number;
  full_name: string;
  email: string;
  phone?: string;
  address?: string;
  avatar?: string;
  is_vip: boolean;
  lifetime_value: number;
  satisfaction_score?: number;
  total_visits: number;
  last_visit_date?: string;
  created_at?: string;
  analytics: GuestAnalytics;
  preferences?: GuestPreference;
  tags: GuestTag[];
  segments: GuestSegment[];
  notes: GuestNote[];
  communications: GuestCommunication[];
  recent_bookings: GuestBooking[];
}

export interface GuestListItem {
  id: number;
  full_name: string;
  email: string;
  phone?: string;
  is_vip: boolean;
  lifetime_value: number;
  satisfaction_score?: number;
  total_visits: number;
  last_visit_date?: string;
  tags: GuestTag[];
  segments: GuestSegment[];
}

export interface GuestSearchParams {
  search?: string;
  is_vip?: boolean;
  segment_id?: number;
  min_lifetime_value?: number;
  min_satisfaction_score?: number;
  tag_id?: number;
  page?: number;
  limit?: number;
}

export interface GuestSearchResponse {
  status: string;
  data: {
    guests: GuestListItem[];
    pagination: {
      total: number;
      page: number;
      limit: number;
      total_pages: number;
    };
  };
}

const guestProfileService = {
  // Search and list guests
  searchGuests: async (params: GuestSearchParams = {}): Promise<GuestSearchResponse> => {
    const response = await apiClient.get('/guest-profiles/', { params });
    return response.data;
  },

  // Get guest profile details
  getGuestProfile: async (userId: number): Promise<{ status: string; data: { profile: GuestProfile } }> => {
    const response = await apiClient.get(`/guest-profiles/${userId}`);
    return response.data;
  },

  // Update guest preferences
  updatePreferences: async (userId: number, preferences: Partial<GuestPreference>): Promise<{ status: string; message: string }> => {
    const response = await apiClient.put(`/guest-profiles/${userId}/preferences`, preferences);
    return response.data;
  },

  // Create guest note
  createNote: async (userId: number, note: { note: string; is_important?: boolean; is_private?: boolean }): Promise<{ status: string; message: string; data: { note: GuestNote } }> => {
    const response = await apiClient.post(`/guest-profiles/${userId}/notes`, note);
    return response.data;
  },

  // Delete guest note
  deleteNote: async (userId: number, noteId: number): Promise<{ status: string; message: string }> => {
    const response = await apiClient.delete(`/guest-profiles/${userId}/notes/${noteId}`);
    return response.data;
  },

  // Toggle VIP status
  toggleVipStatus: async (userId: number, isVip: boolean): Promise<{ status: string; message: string }> => {
    const response = await apiClient.put(`/guest-profiles/${userId}/vip-status`, { is_vip: isVip });
    return response.data;
  },

  // Add tag to guest
  addTag: async (userId: number, tagId: number): Promise<{ status: string; message: string }> => {
    const response = await apiClient.post(`/guest-profiles/${userId}/tags`, { tag_id: tagId });
    return response.data;
  },

  // Remove tag from guest
  removeTag: async (userId: number, tagId: number): Promise<{ status: string; message: string }> => {
    const response = await apiClient.delete(`/guest-profiles/${userId}/tags/${tagId}`);
    return response.data;
  },

  // Create communication record
  createCommunication: async (userId: number, communication: {
    communication_type: string;
    direction: string;
    subject?: string;
    content: string;
    booking_id?: number;
    is_automated?: boolean;
  }): Promise<{ status: string; message: string }> => {
    const response = await apiClient.post(`/guest-profiles/${userId}/communications`, communication);
    return response.data;
  },

  // Get guest analytics
  getAnalytics: async (userId: number): Promise<{ status: string; data: { analytics: GuestAnalytics } }> => {
    const response = await apiClient.get(`/guest-profiles/${userId}/analytics`);
    return response.data;
  },

  // Update guest metrics
  updateMetrics: async (userId: number): Promise<{ status: string; message: string; data: { metrics: any } }> => {
    const response = await apiClient.post(`/guest-profiles/${userId}/update-metrics`);
    return response.data;
  },

  // Get all tags
  getAllTags: async (): Promise<{ status: string; data: { tags: GuestTag[] } }> => {
    const response = await apiClient.get('/guest-profiles/tags/all');
    return response.data;
  },

  // Create tag
  createTag: async (tag: { name: string; color?: string; description?: string }): Promise<{ status: string; message: string; data: { tag: GuestTag } }> => {
    const response = await apiClient.post('/guest-profiles/tags', tag);
    return response.data;
  },

  // Get all segments
  getAllSegments: async (): Promise<{ status: string; data: { segments: GuestSegment[] } }> => {
    const response = await apiClient.get('/guest-profiles/segments/all');
    return response.data;
  },

  // Assign segment to guest
  assignSegment: async (userId: number, segmentId: number): Promise<{ status: string; message: string }> => {
    const response = await apiClient.post(`/guest-profiles/${userId}/segments`, { segment_id: segmentId });
    return response.data;
  },

  // Remove segment from guest
  removeSegment: async (userId: number, segmentId: number): Promise<{ status: string; message: string }> => {
    const response = await apiClient.delete(`/guest-profiles/${userId}/segments/${segmentId}`);
    return response.data;
  },
};

export default guestProfileService;

