/**
 * Type definitions for the Field App
 */

export interface User {
  id: number;
  email: string;
  name: string;
  username?: string;
  role?: string;
  business?: number;
  business_name?: string;
  business_subdomain?: string;
  can_use_masked_calls?: boolean;
  can_edit_schedule?: boolean;
}

export interface AuthResponse {
  access: string;
  refresh: string;
  user: User;
}

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

export type JobStatus =
  | 'SCHEDULED'
  | 'EN_ROUTE'
  | 'IN_PROGRESS'
  | 'COMPLETED'
  | 'CANCELED'
  | 'NOSHOW'
  | 'AWAITING_PAYMENT'
  | 'PAID';

export interface JobListItem {
  id: number;
  title: string;
  start_time: string;
  end_time: string;
  status: JobStatus;
  status_display: string;
  customer_name: string | null;
  address: string | null;
  service_name: string | null;
  duration_minutes: number | null;
}

export interface StatusHistoryItem {
  old_status: JobStatus;
  new_status: JobStatus;
  changed_by: string;
  changed_at: string;
  notes?: string;
}

export interface JobDetail {
  id: number;
  title: string;
  start_time: string;
  end_time: string;
  status: JobStatus;
  status_display: string;
  duration_minutes: number | null;
  customer: {
    id: number;
    name: string;
    email: string | null;
    phone_masked: string | null;
  } | null;
  service: {
    id: number;
    name: string;
    duration: number;
    price: string | null;
  } | null;
  notes: string | null;
  allowed_transitions: JobStatus[];
  can_track_location: boolean;
  has_active_call_session: boolean;
  status_history?: StatusHistoryItem[];
  latest_location?: {
    latitude: number;
    longitude: number;
    timestamp: string;
    accuracy: number | null;
  } | null;
  deposit_amount?: string | null;
  final_price?: string | null;
  created_at: string;
  updated_at: string;
  can_edit_schedule?: boolean;
}

export interface RescheduleRequest {
  start_time?: string;
  end_time?: string;
  duration_minutes?: number;
}

export interface JobsResponse {
  jobs: JobListItem[];
  count: number;
}

export interface StatusChangeRequest {
  status: JobStatus;
  notes?: string;
  latitude?: number;
  longitude?: number;
}

export interface LocationUpdateRequest {
  latitude: number;
  longitude: number;
  accuracy?: number;
  heading?: number;
  speed?: number;
}

export interface LocationPoint {
  latitude: number;
  longitude: number;
  timestamp: string;
  accuracy: number | null;
}

export interface RouteResponse {
  job_id: number;
  route: LocationPoint[];
  point_count: number;
}

export interface CallResponse {
  success: boolean;
  call_sid?: string;
  message?: string;
  error?: string;
}

export interface SMSRequest {
  message: string;
}

export interface SMSResponse {
  success: boolean;
  message_sid?: string;
  error?: string;
}

export interface CallHistoryItem {
  id: number;
  call_type: 'OUTBOUND_CALL' | 'INBOUND_CALL' | 'OUTBOUND_SMS' | 'INBOUND_SMS';
  type_display: string;
  direction: 'outbound' | 'inbound';
  direction_display: string;
  status: string;
  status_display: string;
  duration_seconds: number | null;
  initiated_at: string;
  answered_at: string | null;
  ended_at: string | null;
  employee_name: string | null;
}

export interface ApiError {
  error?: string;
  message?: string;
  detail?: string;
}
