// Paginated response from API
export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  page: number;
  per_page: number;
  total_pages: number;
}

// User types
export interface User {
  id: number;
  username: string;
  email: string;
  first_name: string;
  last_name: string;
  is_active: boolean;
  is_staff: boolean;
  is_superuser: boolean;
  date_joined: string;
  last_login?: string;
  verified: boolean;
  phone_number?: string;
  residence_count: number;
  task_count: number;
}

export interface UserDetail extends User {
  residences?: ResidenceSummary[];
  devices?: DeviceSummary[];
  timezone?: string; // IANA timezone (auto-captured from mobile app)
}

export interface ResidenceSummary {
  id: number;
  name: string;
  is_owner: boolean;
  is_active: boolean;
}

export interface DeviceSummary {
  id: number;
  type: 'apns' | 'gcm';
  name: string;
  is_active: boolean;
}

export interface CreateUserRequest {
  username: string;
  email: string;
  password: string;
  first_name?: string;
  last_name?: string;
  phone_number?: string;
  is_active?: boolean;
  is_staff?: boolean;
  is_superuser?: boolean;
}

export interface UpdateUserRequest {
  username?: string;
  email?: string;
  password?: string;
  first_name?: string;
  last_name?: string;
  phone_number?: string;
  is_active?: boolean;
  is_staff?: boolean;
  is_superuser?: boolean;
  verified?: boolean;
}

// Query params for listing
export interface ListParams {
  page?: number;
  per_page?: number;
  search?: string;
  sort_by?: string;
  sort_dir?: 'asc' | 'desc';
}

export interface UserListParams extends ListParams {
  is_active?: boolean;
  is_staff?: boolean;
  is_superuser?: boolean;
  verified?: boolean;
}

// Residence types
export interface Residence {
  id: number;
  name: string;
  owner_id: number;
  owner_name: string;
  property_type_id?: number;
  property_type?: string;
  street_address: string;
  apartment_unit: string;
  city: string;
  state_province: string;
  postal_code: string;
  country: string;
  bedrooms?: number;
  bathrooms?: number;
  square_footage?: number;
  lot_size?: number;
  year_built?: number;
  description: string;
  purchase_date?: string;
  purchase_price?: number;
  is_primary: boolean;
  is_active: boolean;
  user_count: number;
  created_at: string;
  updated_at: string;
}

export interface ResidenceDetail extends Residence {
  owner?: UserSummary;
  shared_users?: UserSummary[];
  task_count: number;
  document_count: number;
}

export interface UserSummary {
  id: number;
  username: string;
  email: string;
}

export interface ResidenceListParams extends ListParams {
  is_active?: boolean;
  owner_id?: number;
}

export interface CreateResidenceRequest {
  owner_id: number;
  name: string;
  property_type_id?: number;
  street_address?: string;
  apartment_unit?: string;
  city?: string;
  state_province?: string;
  postal_code?: string;
  country?: string;
  bedrooms?: number;
  bathrooms?: number;
  square_footage?: number;
  year_built?: number;
  description?: string;
  is_primary?: boolean;
}

export interface UpdateResidenceRequest {
  owner_id?: number;
  name?: string;
  property_type_id?: number;
  street_address?: string;
  apartment_unit?: string;
  city?: string;
  state_province?: string;
  postal_code?: string;
  country?: string;
  bedrooms?: number;
  bathrooms?: number;
  square_footage?: number;
  lot_size?: number;
  year_built?: number;
  description?: string;
  purchase_date?: string;
  purchase_price?: number;
  is_active?: boolean;
  is_primary?: boolean;
}

// Task types
export interface Task {
  id: number;
  residence_id: number;
  residence_name: string;
  created_by_id: number;
  created_by_name: string;
  assigned_to_id?: number;
  assigned_to_name?: string;
  title: string;
  description: string;
  category_id?: number;
  category_name?: string;
  priority_id?: number;
  priority_name?: string;
  in_progress: boolean;
  frequency_id?: number;
  frequency_name?: string;
  due_date?: string;
  next_due_date?: string;
  estimated_cost?: number;
  actual_cost?: number;
  contractor_id?: number;
  parent_task_id?: number;
  is_cancelled: boolean;
  is_archived: boolean;
  created_at: string;
  updated_at: string;
}

export interface TaskDetail extends Task {
  assigned_to?: UserSummary;
  completion_count: number;
}

export interface TaskListParams extends ListParams {
  residence_id?: number;
  category_id?: number;
  priority_id?: number;
  in_progress?: boolean;
  is_cancelled?: boolean;
  is_archived?: boolean;
}

export interface CreateTaskRequest {
  residence_id: number;
  created_by_id: number;
  title: string;
  description?: string;
  category_id?: number;
  priority_id?: number;
  in_progress?: boolean;
  frequency_id?: number;
  assigned_to_id?: number;
  due_date?: string;
  estimated_cost?: number;
  contractor_id?: number;
}

export interface UpdateTaskRequest {
  residence_id?: number;
  created_by_id?: number;
  assigned_to_id?: number;
  title?: string;
  description?: string;
  category_id?: number;
  priority_id?: number;
  in_progress?: boolean;
  frequency_id?: number;
  due_date?: string;
  next_due_date?: string;
  estimated_cost?: number;
  actual_cost?: number;
  contractor_id?: number;
  parent_task_id?: number;
  is_cancelled?: boolean;
  is_archived?: boolean;
}

// Contractor types
export interface Contractor {
  id: number;
  residence_id: number;
  residence_name: string;
  created_by_id: number;
  created_by_name: string;
  name: string;
  company: string;
  phone: string;
  email: string;
  website: string;
  notes: string;
  street_address: string;
  city: string;
  state_province: string;
  postal_code: string;
  rating?: number;
  specialties?: string[];
  specialty_ids?: number[];
  is_favorite: boolean;
  is_active: boolean;
  created_at: string;
  updated_at: string;
}

export interface ContractorDetail extends Contractor {
  task_count: number;
}

export interface ContractorListParams extends ListParams {
  is_active?: boolean;
  is_favorite?: boolean;
  residence_id?: number;
}

export interface CreateContractorRequest {
  residence_id: number;
  created_by_id: number;
  name: string;
  company?: string;
  phone?: string;
  email?: string;
  website?: string;
  notes?: string;
  street_address?: string;
  city?: string;
  state_province?: string;
  postal_code?: string;
  is_favorite?: boolean;
  specialty_ids?: number[];
}

export interface UpdateContractorRequest {
  residence_id?: number;
  created_by_id?: number;
  name?: string;
  company?: string;
  phone?: string;
  email?: string;
  website?: string;
  notes?: string;
  street_address?: string;
  city?: string;
  state_province?: string;
  postal_code?: string;
  rating?: number;
  is_favorite?: boolean;
  is_active?: boolean;
  specialty_ids?: number[];
}

// Document types
export interface DocumentImage {
  id: number;
  image_url: string;
  caption: string;
}

export interface Document {
  id: number;
  residence_id: number;
  residence_name: string;
  created_by_id: number;
  created_by_name: string;
  title: string;
  description: string;
  document_type: string;
  file_url: string;
  file_name: string;
  file_size?: number;
  mime_type: string;
  purchase_date?: string;
  expiry_date?: string;
  purchase_price?: number;
  vendor: string;
  serial_number: string;
  model_number: string;
  provider: string;
  provider_contact: string;
  claim_phone: string;
  claim_email: string;
  claim_website: string;
  notes: string;
  task_id?: number;
  is_active: boolean;
  images: DocumentImage[];
  created_at: string;
  updated_at: string;
}

export interface DocumentDetail extends Document {
  task_title?: string;
}

export interface DocumentListParams extends ListParams {
  is_active?: boolean;
  residence_id?: number;
  document_type?: string;
}

export interface CreateDocumentRequest {
  residence_id: number;
  created_by_id: number;
  title: string;
  description?: string;
  document_type?: 'general' | 'warranty' | 'receipt' | 'contract' | 'insurance' | 'manual';
  file_url?: string;
  file_name?: string;
  file_size?: number;
  mime_type?: string;
  purchase_date?: string;
  expiry_date?: string;
  purchase_price?: number;
  vendor?: string;
  serial_number?: string;
  model_number?: string;
  task_id?: number;
  image_urls?: string[];
}

export interface UpdateDocumentRequest {
  residence_id?: number;
  created_by_id?: number;
  title?: string;
  description?: string;
  document_type?: string;
  file_url?: string;
  file_name?: string;
  file_size?: number;
  mime_type?: string;
  purchase_date?: string;
  expiry_date?: string;
  purchase_price?: number;
  vendor?: string;
  serial_number?: string;
  model_number?: string;
  provider?: string;
  provider_contact?: string;
  claim_phone?: string;
  claim_email?: string;
  claim_website?: string;
  notes?: string;
  task_id?: number;
  is_active?: boolean;
}

// Notification types
export interface Notification {
  id: number;
  user_id: number;
  user_email: string;
  username: string;
  notification_type: string;
  title: string;
  body: string;
  sent: boolean;
  sent_at?: string;
  read: boolean;
  read_at?: string;
  created_at: string;
}

export interface NotificationDetail extends Notification {
  data: string;
  task_id?: number;
}

export interface NotificationListParams extends ListParams {
  user_id?: number;
  notification_type?: string;
  sent?: boolean;
  read?: boolean;
}

export interface UpdateNotificationRequest {
  title?: string;
  body?: string;
  read?: boolean;
}

export interface NotificationStats {
  total: number;
  sent: number;
  pending: number;
  read: number;
}

// Task Completion types
export interface UpdateCompletionRequest {
  notes?: string;
  actual_cost?: string;
}

// Subscription types
export interface Subscription {
  id: number;
  user_id: number;
  user_email: string;
  username: string;
  tier: 'free' | 'premium' | 'pro';
  platform: string;
  auto_renew: boolean;
  is_free: boolean;
  subscribed_at?: string;
  expires_at?: string;
  cancelled_at?: string;
  created_at: string;
}

export interface SubscriptionDetail extends Subscription {}

export interface SubscriptionListParams extends ListParams {
  user_id?: number;
  tier?: string;
  platform?: string;
  auto_renew?: boolean;
  active?: boolean;
}

export interface UpdateSubscriptionRequest {
  tier?: string;
  auto_renew?: boolean;
  is_free?: boolean;
}

export interface SubscriptionStats {
  total: number;
  free: number;
  premium: number;
  pro: number;
}

// Dashboard types
export interface DashboardStats {
  users: {
    total: number;
    active: number;
    verified: number;
    new_30d: number;
  };
  residences: {
    total: number;
    active: number;
    new_30d: number;
  };
  tasks: {
    total: number;
    active: number;
    archived: number;
    cancelled: number;
    overdue: number;
    new_30d: number;
    pending: number;
    in_progress: number;
    completed: number;
    on_hold: number;
  };
  contractors: {
    total: number;
    active: number;
    favorite: number;
  };
  documents: {
    total: number;
    active: number;
    expired: number;
  };
  notifications: {
    total: number;
    sent: number;
    pending: number;
    read: number;
  };
  subscriptions: {
    total: number;
    free: number;
    premium: number;
    pro: number;
  };
}
