/**
 * Database Types and Interfaces
 * TypeScript definitions matching PostgreSQL table schemas
 */

// Base audit fields present in all tables
export interface AuditFields {
  created_at: Date;
  updated_at: Date;
  synced_at: Date;
  is_deleted: boolean;
  deleted_at?: Date | null;
}

// Company entity
export interface Company extends AuditFields {
  id: number;
  company_name?: string | null;
  company_number?: string | null;
  phone?: string | null;
  fax?: string | null;
  website?: string | null;
  address1?: string | null;
  address2?: string | null;
  city?: string | null;
  state?: string | null;
  postal_code?: string | null;
  country?: string | null;
  is_active?: boolean;
  company_type?: number | null;
  owner_resource_id?: number | null;
  territory_id?: number | null;
  market_segment_id?: number | null;
  competitor_id?: number | null;
  billing_address1?: string | null;
  billing_address2?: string | null;
  billing_city?: string | null;
  billing_state?: string | null;
  billing_postal_code?: string | null;
  billing_country?: string | null;
  tax_id?: string | null;
  tax_exempt?: boolean;
  tax_region_id?: number | null;
  currency_id?: number | null;
  invoice_method?: number | null;
  invoice_template_id?: number | null;
  quote_template_id?: number | null;
  key_account_icon?: number | null;
  last_activity_date?: Date | null;
  last_tracked_modification_date_time?: Date | null;
  api_vendor_id?: number | null;
}

// Resource (User) entity
export interface Resource extends AuditFields {
  id: number;
  first_name?: string | null;
  last_name?: string | null;
  email?: string | null;
  user_name?: string | null;
  title?: string | null;
  office_phone?: string | null;
  mobile_phone?: string | null;
  office_extension?: string | null;
  is_active?: boolean;
  location_id?: number | null;
  resource_type?: number | null;
  pay_roll_identifier?: string | null;
  hire_date?: Date | null;
  travel_availability_pct?: number | null;
  survey_resource_rating?: number | null;
}

// Contact entity
export interface Contact extends AuditFields {
  id: number;
  company_id: number;
  first_name?: string | null;
  last_name?: string | null;
  title?: string | null;
  email_address?: string | null;
  email_address2?: string | null;
  email_address3?: string | null;
  phone?: string | null;
  extension?: string | null;
  alternate_phone?: string | null;
  mobile_phone?: string | null;
  fax?: string | null;
  address_line?: string | null;
  address_line1?: string | null;
  city?: string | null;
  state?: string | null;
  zip_code?: string | null;
  country?: string | null;
  is_active?: boolean;
  name_prefix?: string | null;
  name_suffix?: string | null;
  facebook_url?: string | null;
  twitter_url?: string | null;
  linked_in_url?: string | null;
  primary_contact?: boolean;
  account_physical_location_id?: number | null;
  solicitation_opt_out?: boolean;
  room_number?: string | null;
  last_activity_date?: Date | null;
  last_modified_date?: Date | null;
  api_vendor_id?: number | null;
}

// Project entity
export interface Project extends AuditFields {
  id: number;
  company_id: number;
  project_name?: string | null;
  project_number?: string | null;
  description?: string | null;
  start_date_time?: Date | null;
  end_date_time?: Date | null;
  estimated_time?: number | null;
  actual_hours?: number | null;
  estimated_sale_cost?: number | null;
  labor_estimated_costs?: number | null;
  labor_estimated_revenue?: number | null;
  project_cost_estimated_margin_percentage?: number | null;
  status?: number | null;
  type?: number | null;
  project_lead_resource_id?: number | null;
  account_executive_resource_id?: number | null;
  owner_resource_id?: number | null;
  creator_resource_id?: number | null;
  completed_percentage?: number | null;
  completed_date_time?: Date | null;
  duration?: number | null;
  original_estimated_revenue?: number | null;
  estimated_time_cost?: number | null;
  purchase_order_number?: string | null;
  business_division_subdivision_id?: number | null;
  line_of_business_id?: number | null;
  department?: number | null;
  last_activity_date_time?: Date | null;
  last_activity_person_type?: number | null;
  last_activity_resource_id?: number | null;
}

// Ticket entity
export interface Ticket extends AuditFields {
  id: number;
  company_id: number;
  ticket_number?: string | null;
  title?: string | null;
  description?: string | null;
  status?: number | null;
  priority?: number | null;
  queue_id?: number | null;
  issue_type?: number | null;
  sub_issue_type?: number | null;
  source?: number | null;
  assigned_resource_id?: number | null;
  assigned_resource_role_id?: number | null;
  contact_id?: number | null;
  account_physical_location_id?: number | null;
  due_date_time?: Date | null;
  estimated_hours?: number | null;
  completed_date?: Date | null;
  create_date?: Date | null;
  created_by_contact_id?: number | null;
  last_activity_date?: Date | null;
  last_customer_notification_date_time?: Date | null;
  last_customer_visible_activity_date_time?: Date | null;
  first_response_date_time?: Date | null;
  resolution_plan_date_time?: Date | null;
  resolved_date_time?: Date | null;
  first_response_assigned_resource_id?: number | null;
  first_response_initiating_resource_id?: number | null;
  project_id?: number | null;
  opportunity_id?: number | null;
  change_approval_board?: number | null;
  change_approval_status?: number | null;
  change_approval_type?: number | null;
  change_info_field1?: string | null;
  change_info_field2?: string | null;
  change_info_field3?: string | null;
  change_info_field4?: string | null;
  change_info_field5?: string | null;
  contract_id?: number | null;
  monitor_id?: number | null;
  monitor_type_id?: number | null;
  ticket_type?: number | null;
  ticket_category?: number | null;
  service_level_agreement_id?: number | null;
  resolution?: string | null;
  purchase_order_number?: string | null;
  ticket_completion_date?: Date | null;
  last_activity_person_type?: number | null;
  last_activity_resource_id?: number | null;
  current_service_thermometer_rating?: number | null;
  previous_service_thermometer_rating?: number | null;
  service_thermometer_temperature?: number | null;
  api_vendor_id?: number | null;
}

// Task entity
export interface Task extends AuditFields {
  id: number;
  title?: string | null;
  description?: string | null;
  status?: number | null;
  priority?: number | null;
  assigned_resource_id?: number | null;
  assigned_resource_role_id?: number | null;
  department_id?: number | null;
  estimated_hours?: number | null;
  remaining_hours?: number | null;
  hours_to_be_scheduled?: number | null;
  start_date_time?: Date | null;
  end_date_time?: Date | null;
  completed_date_time?: Date | null;
  create_date_time?: Date | null;
  creator_resource_id?: number | null;
  completed_by_resource_id?: number | null;
  last_activity_date_time?: Date | null;
  project_id?: number | null;
  ticket_id?: number | null;
  phase_id?: number | null;
  allocation_code_id?: number | null;
  task_type?: number | null;
  task_is_billable?: boolean;
  task_number?: string | null;
  purchase_order_number?: string | null;
  can_client_portal_user_complete_task?: boolean;
  creator_type?: number | null;
  task_category_id?: number | null;
}

// Configuration Item entity
export interface ConfigurationItem extends AuditFields {
  id: number;
  company_id: number;
  product_id?: number | null;
  reference_title?: string | null;
  reference_number?: string | null;
  serial_number?: string | null;
  install_date?: Date | null;
  warranty_expiration_date?: Date | null;
  is_active?: boolean;
  daily_cost?: number | null;
  hourly_cost?: number | null;
  monthly_cost?: number | null;
  per_use_cost?: number | null;
  setup_fee?: number | null;
  contact_id?: number | null;
  location_id?: number | null;
  vendor_id?: number | null;
  installed_by_id?: number | null;
  installed_by_contact_id?: number | null;
  parent_configuration_item_id?: number | null;
  notes?: string | null;
  create_date?: Date | null;
  created_by_person_id?: number | null;
  last_modified_time?: Date | null;
  last_activity_person_type?: number | null;
  impersonator_creator_resource_id?: number | null;
  configuration_item_category_id?: number | null;
  configuration_item_type?: number | null;
  datto_availability?: number | null;
  datto_device_memory_megabytes?: number | null;
  datto_drives_errors?: boolean | null;
  datto_hostname?: string | null;
  datto_internal_ip?: string | null;
  datto_kernel_version_id?: number | null;
  datto_last_check_in_date_time?: Date | null;
  datto_nic_speed_kilobits_per_second?: number | null;
  datto_number_of_agents?: number | null;
  datto_number_of_drives?: number | null;
  datto_number_of_logical_volumes?: number | null;
  datto_number_of_volumes?: number | null;
  datto_off_site_storage_used_bytes?: number | null;
  datto_os_version_id?: number | null;
  datto_percentage_used?: number | null;
  datto_protected_kilobytes?: number | null;
  datto_remote_ip?: string | null;
  datto_serial_number?: string | null;
  datto_uptime_seconds?: number | null;
  datto_used_kilobytes?: number | null;
  datto_z_pool_percentage?: number | null;
  device_networking_id?: number | null;
  last_backup_date?: Date | null;
  last_backup_status?: number | null;
  os_version_id?: number | null;
  service_id?: number | null;
  service_bundle_id?: number | null;
  snmp_location?: string | null;
  snmp_name?: string | null;
  snmp_contact?: string | null;
  api_vendor_id?: number | null;
  device_type?: string | null;
  rmm_device_uid?: string | null;
  rmm_device_audit_architecture_id?: number | null;
  rmm_device_audit_display_adaptor_id?: number | null;
  rmm_device_audit_domain_id?: number | null;
  rmm_device_audit_external_ip_address?: string | null;
  rmm_device_audit_hostname?: string | null;
  rmm_device_audit_ip_address?: string | null;
  rmm_device_audit_mac_address?: string | null;
  rmm_device_audit_manufacturer_id?: number | null;
  rmm_device_audit_missing_patch_count?: number | null;
  rmm_device_audit_mobile_network_operator_id?: number | null;
  rmm_device_audit_mobile_number?: string | null;
  rmm_device_audit_model_id?: number | null;
  rmm_device_audit_motherboard_id?: number | null;
  rmm_device_audit_operating_system_id?: number | null;
  rmm_device_audit_processor_id?: number | null;
  rmm_device_audit_service_pack_id?: number | null;
  rmm_device_audit_snmp_contact?: string | null;
  rmm_device_audit_snmp_location?: string | null;
  rmm_device_audit_snmp_name?: string | null;
  rmm_device_audit_software_status_id?: number | null;
  rmm_device_audit_storage_bytes?: number | null;
  rmm_open_alert_count?: number | null;
  rmm_device_audit_description?: string | null;
  rmm_device_audit_device_type_id?: number | null;
  rmm_device_audit_last_user?: string | null;
  rmm_device_audit_memory_bytes?: number | null;
  source_cost_id?: number | null;
  source_cost_type?: number | null;
}

// Contract entity
export interface Contract extends AuditFields {
  id: number;
  company_id: number;
  contract_name?: string | null;
  contract_number?: string | null;
  description?: string | null;
  start_date?: Date | null;
  end_date?: Date | null;
  time_reporting_requires_start_and_stop_times?: number | null;
  service_level_agreement_id?: number | null;
  contract_type?: number | null;
  contract_category?: number | null;
  status?: number | null;
  business_division_subdivision_id?: number | null;
  contact_id?: number | null;
  contact_name?: string | null;
  billing_preference?: number | null;
  purchase_order_number?: string | null;
  setup_fee?: number | null;
  setup_fee_allocation_code_id?: number | null;
  estimated_cost?: number | null;
  estimated_hours?: number | null;
  estimated_revenue?: number | null;
  over_budget_dollar_amount?: number | null;
  over_budget_hours?: number | null;
  contract_period_type?: string | null;
  opportunity_id?: number | null;
  renewed_contract_id?: number | null;
  is_default_contract?: boolean;
  internal_currency_setup_fee?: number | null;
  internal_currency_over_budget_dollar_amount?: number | null;
  internal_currency_estimated_cost?: number | null;
  internal_currency_estimated_revenue?: number | null;
  exclusion_contract_id?: number | null;
  internal_currency_monthly_revenue?: number | null;
  internal_currency_quarterly_revenue?: number | null;
  internal_currency_semi_annual_revenue?: number | null;
  internal_currency_yearly_revenue?: number | null;
  internal_currency_one_time_revenue?: number | null;
  compliance?: boolean | null;
}

// Billing Item entity
export interface BillingItem extends AuditFields {
  id: number;
  company_id?: number | null;
  product_id?: number | null;
  description?: string | null;
  quantity?: number | null;
  rate?: number | null;
  total_amount?: number | null;
  line_discount_dollars?: number | null;
  line_discount_percent?: number | null;
  tax_category_id?: number | null;
  internal_currency_line_discount_dollars?: number | null;
  allocation_code_id?: number | null;
  invoice_id?: number | null;
  vendor_id?: number | null;
  expense_item?: boolean;
  task_id?: number | null;
  ticket_id?: number | null;
  project_id?: number | null;
  our_cost?: number | null;
  list_price?: number | null;
  unit_cost?: number | null;
  unit_price?: number | null;
  extended_price?: number | null;
  tax_dollars?: number | null;
  internal_currency_unit_price?: number | null;
  internal_currency_total_amount?: number | null;
}

// Picklist base interface
export interface PicklistValue extends AuditFields {
  value: number;
  label: string;
  is_active?: boolean;
  is_system?: boolean;
  sort_order?: number | null;
  parent_value?: number | null;
}

// Status picklist
export interface Status extends PicklistValue {}

// Issue Type picklist
export interface IssueType extends PicklistValue {}

// Sub-Issue Type picklist
export interface SubIssueType extends PicklistValue {}

// Work Type picklist
export interface WorkType extends PicklistValue {}

// Sync History (matches sync_history table)
export interface SyncHistoryRecord {
  id: number;
  entity_type: string;
  sync_type: 'full' | 'incremental' | 'entity-specific';
  status: 'started' | 'in_progress' | 'completed' | 'failed';
  started_at: Date;
  completed_at?: Date | null;
  records_added: number;
  records_updated: number;
  records_deleted: number;
  error_message?: string | null;
  triggered_by?: string | null;
}

// Device Lifecycle Policy entity
export interface DeviceLifecyclePolicy extends AuditFields {
  id: number;
  device_type: string;
  expected_months: number;
}

// Time Entry entity
export interface TimeEntry extends AuditFields {
  id: number;
  resource_id: number;
  ticket_id?: number | null;
  task_id?: number | null;
  project_id?: number | null;
  company_id?: number | null;
  entry_date: Date;
  hours_worked: number;
  notes?: string | null;
  internal_notes?: string | null;
  title?: string | null;
  type?: number | null;
  start_date_time?: Date | null;
  end_date_time?: Date | null;
  billable?: boolean;
  billing_rate?: number | null;
  billing_rate_currency_id?: number | null;
  cost_rate?: number | null;
  cost_rate_currency_id?: number | null;
  cost?: number | null;
  cost_currency_id?: number | null;
  revenue?: number | null;
  revenue_currency_id?: number | null;
  margin?: number | null;
  margin_currency_id?: number | null;
  approved?: boolean;
  approved_by_resource_id?: number | null;
  approved_date_time?: Date | null;
  non_billable?: boolean;
  contract_service_id?: number | null;
  contract_service_bundle_id?: number | null;
  role_id?: number | null;
  department_id?: number | null;
  location_id?: number | null;
  allocation_code_id?: number | null;
  imp_project_schedule_id?: number | null;
  imp_project_schedule_task_id?: number | null;
  api_vendor_id?: number | null;
}

// Union type for all entities
export type Entity =
  | Company
  | Resource
  | Contact
  | Project
  | Ticket
  | Task
  | ConfigurationItem
  | Contract
  | BillingItem
  | Status
  | IssueType
  | SubIssueType
  | WorkType
  | TimeEntry
  | DeviceLifecyclePolicy;

export type TableName =
  | 'companies'
  | 'resources'
  | 'contacts'
  | 'projects'
  | 'tickets'
  | 'tasks'
  | 'configuration_items'
  | 'contracts'
  | 'billing_items'
  | 'statuses'
  | 'issue_types'
  | 'sub_issue_types'
  | 'work_types'
  | 'time_entries'
  | 'sync_history'
  | 'device_lifecycle_policies';
