export type WorkspaceType = 'standard' | 'rescue';
export type WorkspaceRole = 'owner' | 'assistant' | 'caregiver' | 'viewer';
export type BillingPlan = 'rescue_free' | 'household_basic' | 'household_plus' | 'household_macaw' | 'household_hyacinth_macaw';
export type BillingInterval = 'monthly' | 'yearly';
export type SubscriptionStatus = 'active' | 'trialing' | 'past_due' | 'canceled' | 'unpaid' | 'none';
export type RescueVerificationStatus = 'not_required' | 'pending' | 'approved' | 'rejected';
export type ProviderKey = 'google' | 'microsoft' | 'apple';
export type IntegrationTokenScope = 'read_only' | 'read_write';
export type BirdGender = 'unknown' | 'male' | 'female';

export type UserRow = {
  id: string;
  email: string;
  password_hash: string | null;
  name: string;
  education_opt_out?: boolean;
  created_at: string;
};

export type DailyEducationQuestion = {
  prompt: string;
  options: string[];
  correctAnswerIndex: number;
  explanation: string | null;
};

export type DailyEducationRow = {
  id: string;
  publish_date: string;
  fact: string;
  quiz_questions: DailyEducationQuestion[];
  created_by_user_id: string | null;
  created_at: string;
  updated_at: string;
};

export type EducationQuestionRow = {
  id: string;
  prompt: string;
  options: string[];
  correct_answer_index: number;
  explanation: string | null;
  created_by_user_id: string | null;
  created_at: string;
  updated_at: string;
};

export type WorkspaceRow = {
  id: number;
  name: string;
  workspace_type: WorkspaceType;
  billing_email: string | null;
  billing_plan: BillingPlan;
  billing_interval: BillingInterval;
  subscription_status: SubscriptionStatus;
  stripe_customer_id: string | null;
  stripe_subscription_id: string | null;
  rescue_verification_status: RescueVerificationStatus;
  created_at: string;
  updated_at: string;
};

export type WorkspaceMemberRow = {
  id: string;
  workspace_id: number;
  user_id: string | null;
  invite_email: string;
  name: string;
  role: WorkspaceRole;
  accepted_at: string | null;
  created_at: string;
};

export type AuthSessionRow = {
  id: string;
  user_id: string;
  active_workspace_id: number;
  token_hash: string;
  expires_at: string;
  created_at: string;
};

export type AuthAccountRow = {
  id: string;
  user_id: string;
  provider_key: ProviderKey;
  provider_subject: string;
  provider_email: string | null;
  created_at: string;
};

export type OAuthStateRow = {
  id: string;
  provider_key: ProviderKey;
  code_verifier: string;
  redirect_to: string;
  expires_at: string;
};

export type MagicLinkTokenRow = {
  id: string;
  email: string;
  name: string | null;
  token_hash: string;
  redirect_to: string;
  expires_at: string;
  created_at: string;
};

export type IntegrationTokenRow = {
  id: string;
  user_id: string;
  workspace_id: number;
  name: string;
  token_hash: string;
  token_prefix: string;
  scope: IntegrationTokenScope;
  last_used_at: string | null;
  expires_at: string | null;
  revoked_at: string | null;
  created_at: string;
};

export type BirdRow = {
  id: string;
  workspace_id: number;
  name: string;
  tag_id: string | null;
  species: string;
  motivators: string | null;
  demotivators: string | null;
  favorite_snack: string | null;
  vet_clinic_name: string | null;
  vet_clinic_address: string | null;
  vet_account_number: string | null;
  vet_doctor_name: string | null;
  gender: BirdGender;
  date_of_birth: string | null;
  gotcha_day: string | null;
  chart_color: string;
  photo_data_url: string | null;
  photo_object_key: string | null;
  photo_content_type: string | null;
  photo_updated_at: string | null;
  notify_on_dob: boolean;
  notify_on_gotcha_day: boolean;
  public_profile_code: string | null;
  public_profile_enabled: boolean;
  memorialized_at: string | null;
  memorialized_on: string | null;
  memorial_note: string | null;
  notify_on_memorial_day: boolean;
  created_at: string;
  latest_weight_grams: string | null;
  latest_recorded_on: string | null;
};

export type LostBirdMatchRow = BirdRow & {
  workspace_name: string;
  workspace_billing_email: string | null;
};

export type BirdMilestoneReminderType = 'hatch_day' | 'gotcha_day' | 'memorial_day';

export type BirdMilestoneReminderCandidateRow = BirdRow & {
  workspace_name: string;
  reminder_type: BirdMilestoneReminderType;
  reminder_date: string;
  reminder_year: number;
};

export type BirdMilestoneReminderDeliveryRow = {
  id: string;
  bird_id: string;
  workspace_id: number;
  reminder_type: BirdMilestoneReminderType;
  reminder_year: number;
  delivered_on: string;
  created_at: string;
};

export type PendingBirdTransferRow = {
  id: string;
  bird_id: string;
  source_workspace_id: number;
  destination_owner_email: string;
  requested_by_user_id: string;
  completed_at: string | null;
  completed_workspace_id: number | null;
  last_error: string | null;
  created_at: string;
};

export type BirdTransferCodeRow = {
  id: string;
  code: string;
  bird_id: string;
  source_workspace_id: number;
  requested_by_user_id: string;
  completed_at: string | null;
  completed_workspace_id: number | null;
  revoked_at: string | null;
  created_at: string;
};

export type WeightRow = {
  id: string;
  bird_id: string;
  weight_grams: string;
  recorded_on: string;
  notes: string | null;
};

export type VetVisitRow = {
  id: string;
  bird_id: string;
  visited_on: string;
  clinic_name: string;
  reason: string;
  notes: string | null;
};

export type MedicationRow = {
  id: string;
  bird_id: string;
  name: string;
  dosage: string;
  frequency: string;
  dose_schedule: MedicationDoseScheduleItem[];
  route: string | null;
  start_date: string;
  end_date: string | null;
  notes: string | null;
};

export type MedicationDoseScheduleItem = {
  key: string;
  label: string;
  time: string;
};

export type MedicationAdministrationRow = {
  id: string;
  medication_id: string;
  bird_id: string;
  administered_on: string;
  administration_slot: string;
  status: 'administered' | 'missed';
  notes: string | null;
  created_by_user_id: string | null;
  created_at: string;
};

export type FlockNoteRow = {
  id: string;
  workspace_id: number;
  bird_id: string | null;
  bird_name: string | null;
  title: string;
  body: string;
  created_by_user_id: string | null;
  created_by_name: string | null;
  created_at: string;
  updated_at: string;
};

export type AuditLogEntryRow = {
  id: string;
  workspace_id: number;
  user_id: string | null;
  actor_name: string | null;
  actor_email: string | null;
  action: string;
  entity_type: string;
  entity_id: string | null;
  entity_name: string | null;
  details: Record<string, unknown>;
  created_at: string;
};

export type AuthContext = {
  user: UserRow;
  session: AuthSessionRow;
  workspace: WorkspaceRow;
  membership: WorkspaceMemberRow;
  token: string;
  authType: 'session' | 'integration_token';
  integrationToken?: IntegrationTokenRow;
};
