export interface User {
  id: string;
  name: string;
  email: string;
  phone: string | null;
  profile_photo: string | null;
  country_of_residence: string | null;
  city: string | null;
  nationality: string | null;
  avatar: string | null;
  is_active: boolean;
  email_verified_at: string | null;
  two_factor_method: 'totp' | 'email' | null;
  two_factor_confirmed: boolean;
  created_at: string;
  roles: (string | Role)[];
  permissions?: string[];
}

/** Normalize a role (which may be a string or an object) to its name string */
export function getRoleName(role: string | Role): string {
  return typeof role === 'string' ? role : role.name;
}

/** Normalize an array of roles to string names */
export function getRoleNames(roles: (string | Role)[]): string[] {
  return roles.map(getRoleName);
}

export interface Role {
  id: string;
  name: string;
  guard_name: string;
  permissions?: Permission[];
  users_count?: number;
}

export interface Permission {
  id: string;
  name: string;
  guard_name: string;
}

export interface AuthResponse {
  message: string;
  user: User;
  token: string;
  two_factor?: boolean;
  two_factor_method?: 'totp' | 'email';
}

export interface RegisterData {
  name: string;
  email: string;
  password: string;
  password_confirmation: string;
  phone?: string;
  country_of_residence?: string;
  city?: string;
}

export interface LoginData {
  email: string;
  password: string;
}

// Pagination
export interface PaginatedResponse<T> {
  data: T[];
  meta: {
    current_page: number;
    last_page: number;
    per_page: number;
    total: number;
    from: number | null;
    to: number | null;
  };
  links: {
    first: string | null;
    last: string | null;
    prev: string | null;
    next: string | null;
  };
}

// Notifications
export interface Notification {
  id: string;
  type: string;
  data: {
    title: string;
    message: string;
    action_url?: string;
    icon?: string;
  };
  read_at: string | null;
  created_at: string;
}

// Company
export interface CompanyCategory {
  id: string;
  name: string;
  slug: string;
  description: string | null;
  icon: string | null;
  sort_order: number;
  companies_count?: number;
  meta: { title: string; description: string | null };
}

export interface Company {
  id: string;
  name: string;
  slug: string;
  short_description: string | null;
  description?: string | null;
  logo: string | null;
  logo_thumbnail: string | null;
  cover_image: string | null;
  cover_thumbnail: string | null;
  phone?: string | null;
  email?: string | null;
  website: string | null;
  address?: string | null;
  city: string | null;
  county: string | null;
  country: string | null;
  location?: { latitude: number | null; longitude: number | null };
  is_featured: boolean;
  is_verified: boolean;
  status: "draft" | "pending" | "published" | "suspended";
  views_count: number;
  category_id?: string;
  category?: CompanyCategory;
  meta_title?: string;
  meta_description?: string;
  meta: { title: string; description: string | null; og_image: string | null };
  created_at: string;
}

// Blog
export interface BlogCategory {
  id: string;
  name: string;
  slug: string;
  description: string | null;
  sort_order: number;
  posts_count?: number;
  meta: { title: string; description: string | null };
}

export interface Tag {
  id: string;
  name: string;
  slug: string;
}

export interface Post {
  id: string;
  title: string;
  slug: string;
  excerpt: string | null;
  content?: string | null;
  body?: string;
  featured_image: string | null;
  featured_image_thumbnail: string | null;
  featured_image_alt: string | null;
  status: "draft" | "pending" | "published" | "archived";
  is_featured: boolean;
  views_count: number;
  reading_time_minutes: number;
  published_at: string | null;
  category_id?: string;
  category?: BlogCategory;
  author?: { name: string; avatar: string | null };
  tags?: Tag[];
  meta_title?: string;
  meta_description?: string;
  meta: { title: string; description: string | null; og_image: string | null };
  created_at: string;
}

// Property
export interface PropertyCategory {
  id: string;
  name: string;
  slug: string;
  description: string | null;
  icon: string | null;
  sort_order: number;
  is_active: boolean;
  properties_count?: number;
  meta: { title: string; description: string | null };
}

export interface Amenity {
  id: string;
  name: string;
  slug: string;
  icon: string | null;
  group: string | null;
}

export interface PropertyImage {
  id: string;
  url: string;
  thumbnail: string | null;
  alt_text: string | null;
  sort_order: number;
  is_primary: boolean;
}

export interface Property {
  id: string;
  title: string;
  slug: string;
  short_description: string | null;
  description?: string | null;
  price: string | number | null;
  currency: string;
  price_period: "one_time" | "monthly" | "yearly";
  listing_type: "sale" | "rent" | "lease";
  property_type: string;
  bedrooms: number | null;
  bathrooms: number | null;
  area_sqft: number | null;
  plot_size_sqft?: number | null;
  city: string | null;
  county: string | null;
  country: string | null;
  address?: string | null;
  latitude?: number | null;
  longitude?: number | null;
  location?: { latitude: number | null; longitude: number | null };
  year_built: number | null;
  is_featured: boolean;
  is_negotiable: boolean;
  status: "draft" | "pending" | "published" | "sold" | "rented" | "expired";
  views_count: number;
  published_at: string | null;
  expires_at: string | null;
  category_id?: string;
  category?: PropertyCategory;
  owner?: { name: string; phone?: string | null };
  images?: PropertyImage[];
  amenities?: Amenity[];
  meta_title?: string;
  meta_description?: string;
  meta: { title: string; description: string | null; og_image: string | null };
  created_at: string;
}

// Magazine
export interface MagazineCategory {
  id: string;
  name: string;
  slug: string;
  description: string | null;
  sort_order: number;
  articles_count?: number;
}

export interface MagazineIssue {
  id: string;
  title: string;
  slug: string;
  description: string | null;
  cover_image: string | null;
  cover_thumbnail: string | null;
  has_pdf: boolean;
  pdf_file_name: string | null;
  pdf_file_size: number | null;
  pdf_page_count: number | null;
  volume_number: number | null;
  issue_number: number;
  is_free: boolean;
  price: string | null;
  currency: string;
  status: "draft" | "published" | "archived";
  published_at: string | null;
  articles_count?: number;
  articles?: MagazineArticle[];
  meta: { title: string; description: string | null; og_image: string | null };
  created_at: string;
}

export interface MagazineArticle {
  id: string;
  title: string;
  slug: string;
  excerpt: string | null;
  content?: string | null;
  body?: string;
  is_locked?: boolean;
  featured_image: string | null;
  featured_image_thumbnail: string | null;
  is_free: boolean;
  sort_order: number;
  reading_time_minutes: number;
  views_count: number;
  status: "draft" | "published" | "archived";
  published_at: string | null;
  issue_id?: string;
  category_id?: string;
  category?: MagazineCategory;
  issue?: { title: string; slug: string; issue_number: number };
  author?: { name: string };
  meta: { title: string; description: string | null; og_image: string | null };
}

// Magazine Subscriptions
export interface Subscription {
  id: string;
  user_id: number;
  plan: string;
  amount_paid: string;
  currency: string;
  payment_reference: string | null;
  payment_method: string | null;
  status: "active" | "cancelled" | "expired";
  starts_at: string;
  ends_at: string | null;
  cancelled_at: string | null;
  created_at: string;
  user?: { id: number; name: string; email: string };
}

// Advertising
export interface AdPlacement {
  id: string;
  name: string;
  slug: string;
  description: string | null;
  dimensions: string | null;
  page_location: string | null;
  pricing: {
    per_day: string;
    per_week: string;
    per_month: string;
    currency: string;
  };
  is_active: boolean;
  advertisements_count?: number;
}

export interface Advertisement {
  id: string;
  title: string;
  company_name: string | null;
  advertiser_name: string | null;
  advertiser_email: string | null;
  image_url: string;
  image_thumbnail: string | null;
  target_url: string;
  alt_text: string | null;
  starts_at: string;
  ends_at: string;
  status: "pending" | "active" | "paused" | "expired" | "rejected" | "draft";
  impressions_count?: number;
  clicks_count?: number;
  stats?: { impressions: number; clicks: number; ctr: number };
  total_cost?: string;
  currency: string;
  placement_id?: string;
  placement?: AdPlacement;
  rejection_reason?: string | null;
  created_at: string;
}
