-- Add missing fields to tickets table based on Autotask Tickets API
-- Reference: https://autotask.net/help/developerhelp/Content/APIs/REST/Entities/TicketsEntity.htm

ALTER TABLE tickets
  -- Billing and configuration
  ADD COLUMN IF NOT EXISTS billing_code_id BIGINT,
  ADD COLUMN IF NOT EXISTS configuration_item_id BIGINT,
  
  -- Creator information
  ADD COLUMN IF NOT EXISTS creator_resource_id BIGINT,
  ADD COLUMN IF NOT EXISTS creator_type INTEGER,
  
  -- Problem/incident tracking
  ADD COLUMN IF NOT EXISTS problem_ticket_id BIGINT,
  
  -- RMA fields
  ADD COLUMN IF NOT EXISTS rma_status INTEGER,
  ADD COLUMN IF NOT EXISTS rma_type INTEGER,
  
  -- SLA tracking
  ADD COLUMN IF NOT EXISTS service_level_agreement_paused_next_event_hours DECIMAL(10,2),
  
  -- Co-managed fields
  ADD COLUMN IF NOT EXISTS is_assigned_to_comanaged BOOLEAN DEFAULT false,
  ADD COLUMN IF NOT EXISTS is_visible_to_comanaged BOOLEAN DEFAULT false;

-- Add comment to track migration
COMMENT ON TABLE tickets IS 'Added missing Autotask API fields - Migration 016';
