import type { SupportLevel } from './api';

// --- Enums ---

export type VisitOutcome =
  | 'NOT_HOME'
  | 'REFUSED'
  | 'MOVED'
  | 'ALREADY_VOTED'
  | 'SPOKE_WITH'
  | 'LEFT_LITERATURE'
  | 'COME_BACK_LATER';

export type CanvassSessionStatus = 'ACTIVE' | 'COMPLETED' | 'ABANDONED';

export const VISIT_OUTCOME_LABELS: Record<VisitOutcome, string> = {
  NOT_HOME: 'Not Home',
  REFUSED: 'Refused',
  MOVED: 'Moved',
  ALREADY_VOTED: 'Already Voted',
  SPOKE_WITH: 'Spoke With',
  LEFT_LITERATURE: 'Left Literature',
  COME_BACK_LATER: 'Come Back Later',
};

export const VISIT_OUTCOME_COLORS: Record<VisitOutcome, string> = {
  NOT_HOME: '#f1c40f',
  REFUSED: '#e74c3c',
  MOVED: '#95a5a6',
  ALREADY_VOTED: '#9b59b6',
  SPOKE_WITH: '#27ae60',
  LEFT_LITERATURE: '#3498db',
  COME_BACK_LATER: '#e67e22',
};

// --- Interfaces ---

export interface Location {
  id: string;
  latitude: number;
  longitude: number;
  address: string;
  buildingNotes: string | null;
}

export interface Address {
  id: string;
  locationId: string;
  unitNumber: string | null;
  firstName: string | null;
  lastName: string | null;
  email: string | null;
  phone: string | null;
  supportLevel: SupportLevel | null;
  sign: boolean;
  signSize: string | null;
  notes: string | null;
}

export interface CanvassAddress extends Address {
  location: Location;
  lastVisit: {
    outcome: VisitOutcome;
    visitedAt: string;
    visitorName: string | null;
    isMyVisit: boolean;
  } | null;
}

export interface AddressGroup {
  locationId: string;
  latitude: number;
  longitude: number;
  baseAddress: string;
  buildingNotes: string | null;
  addresses: CanvassAddress[];
  isMultiUnit: boolean;
}

// Deprecated: kept for backward compatibility, use CanvassAddress instead
export interface CanvassLocation {
  id: string;
  latitude: number;
  longitude: number;
  address: string | null;
  unitNumber: string | null;
  firstName: string | null;
  lastName: string | null;
  supportLevel: SupportLevel | null;
  sign: boolean;
  signSize: string | null;
  notes: string | null;
  lastVisit: {
    outcome: VisitOutcome;
    visitedAt: string;
    visitorName: string | null;
    isMyVisit: boolean;
  } | null;
}

export interface WalkingRoute {
  orderedLocations: { id: string; latitude: number; longitude: number }[];
  totalDistanceMeters: number;
  estimatedMinutes: number;
}

export interface CanvassSession {
  id: string;
  userId: string;
  cutId: string;
  shiftId: string | null;
  status: CanvassSessionStatus;
  startedAt: string;
  endedAt: string | null;
  startLatitude: string | null;
  startLongitude: string | null;
  cut?: { id: string; name: string };
  shift?: { id: string; title: string } | null;
}

export interface MyAssignment {
  shiftId: string;
  shiftTitle: string;
  shiftDate: string;
  startTime: string;
  endTime: string;
  location: string | null;
  cutId: string;
  cutName: string;
  completionPercentage: number;
}

export interface MyCanvassStats {
  totalVisits: number;
  todayVisits: number;
  byOutcome: Record<string, number>;
  sessions: number;
}

export interface RecordVisitPayload {
  addressId: string; // Changed from locationId
  outcome: VisitOutcome;
  supportLevel?: SupportLevel;
  signRequested?: boolean;
  signSize?: string;
  notes?: string;
  durationSeconds?: number;
  sessionId?: string;
  shiftId?: string;
  updateLocation?: boolean;
}

export interface BulkRecordVisitPayload {
  locationId: string; // Building ID
  outcome: 'NOT_HOME' | 'REFUSED' | 'MOVED'; // Only non-contact outcomes
  notes?: string;
  sessionId?: string;
  shiftId?: string;
}

export interface CanvassVisit {
  id: string;
  addressId: string; // Changed from locationId
  userId: string;
  outcome: VisitOutcome;
  supportLevel: SupportLevel | null;
  signRequested: boolean;
  signSize: string | null;
  notes: string | null;
  durationSeconds: number | null;
  visitedAt: string;
  address?: {
    id: string;
    unitNumber: string | null;
    location: { address: string };
  };
  user?: { id: string; name: string | null; email: string };
}

// --- Admin types ---

export interface CanvassAdminStats {
  totalVisits: number;
  todayVisits: number;
  activeSessions: number;
  activeVolunteers: number;
  overallCompletion: number;
}

export interface CutCanvassStats {
  cutId: string;
  cutName: string;
  totalLocations: number;
  visitedLocations: number;
  completionPercentage: number;
  totalVisits: number;
  byOutcome: Record<string, number>;
}

export interface VolunteerSummary {
  userId: string;
  name: string | null;
  email: string;
  totalVisits: number;
  sessions: number;
  lastActive: string | null;
}

// --- Outcome Trends ---

export type CanvassOutcomeTrendPoint = { date: string } & Partial<Record<VisitOutcome, number>>;

export interface CanvassOutcomeTrendsData {
  granularity: 'day' | 'week';
  dateFrom: string;
  dateTo: string;
  series: CanvassOutcomeTrendPoint[];
  totals: Partial<Record<VisitOutcome, number>>;
}
