-- Migration 001: Create custom PostgreSQL types (enums)
-- These types must be created before any tables that reference them

-- Enable btree_gist extension for exclusion constraints (non-overlapping date ranges)
CREATE EXTENSION IF NOT EXISTS btree_gist;

-- Entity lifecycle status
CREATE TYPE entity_status AS ENUM (
    'ACTIVE',
    'INACTIVE',
    'PENDING'
);

-- Work item status (services, projects)
CREATE TYPE work_status AS ENUM (
    'SCHEDULED',
    'IN_PROGRESS',
    'COMPLETED',
    'CANCELLED'
);

-- Invoice lifecycle status
CREATE TYPE invoice_status AS ENUM (
    'DRAFT',
    'SENT',
    'PAID',
    'OVERDUE',
    'CANCELLED'
);

-- Payment method types
CREATE TYPE payment_type AS ENUM (
    'CHECK',
    'CREDIT_CARD',
    'BANK_TRANSFER',
    'CASH'
);

-- Task frequency for scope tasks
CREATE TYPE task_frequency AS ENUM (
    'DAILY',
    'WEEKLY',
    'MONTHLY',
    'QUARTERLY',
    'TRIANNUAL',
    'ANNUAL',
    'AS_NEEDED'
);

-- Team member role hierarchy
CREATE TYPE team_role AS ENUM (
    'ADMIN',
    'TEAM_LEADER',
    'TEAM_MEMBER'
);

-- Conversation types for messaging
CREATE TYPE conversation_type AS ENUM (
    'DIRECT',
    'GROUP',
    'SUPPORT'
);

-- Notification delivery channels
CREATE TYPE notification_channel AS ENUM (
    'IN_APP',
    'EMAIL',
    'SMS'
);

-- Notification status
CREATE TYPE notification_status AS ENUM (
    'PENDING',
    'SENT',
    'READ',
    'FAILED'
);

-- Delivery attempt status
CREATE TYPE delivery_status AS ENUM (
    'PENDING',
    'QUEUED',
    'SENDING',
    'SENT',
    'DELIVERED',
    'FAILED',
    'BOUNCED'
);

-- Comprehensive event types for audit trail
CREATE TYPE event_type AS ENUM (
    -- Customer events
    'CUSTOMER_CREATED',
    'CUSTOMER_UPDATED',
    'CUSTOMER_DELETED',
    'CUSTOMER_STATUS_CHANGED',
    'CUSTOMER_ADDRESS_CREATED',
    'CUSTOMER_ADDRESS_UPDATED',
    'CUSTOMER_ADDRESS_DELETED',
    'CUSTOMER_CONTACT_CREATED',
    'CUSTOMER_CONTACT_UPDATED',
    'CUSTOMER_CONTACT_DELETED',

    -- Account events
    'ACCOUNT_CREATED',
    'ACCOUNT_UPDATED',
    'ACCOUNT_DELETED',
    'ACCOUNT_STATUS_CHANGED',
    'ACCOUNT_ADDRESS_CREATED',
    'ACCOUNT_ADDRESS_UPDATED',
    'ACCOUNT_ADDRESS_DELETED',
    'ACCOUNT_ADDRESS_PRIMARY_CHANGED',
    'ACCOUNT_CONTACT_CREATED',
    'ACCOUNT_CONTACT_UPDATED',
    'ACCOUNT_CONTACT_DELETED',
    'ACCOUNT_CONTACT_PRIMARY_CHANGED',

    -- Service events
    'SERVICE_CREATED',
    'SERVICE_UPDATED',
    'SERVICE_DELETED',
    'SERVICE_STATUS_CHANGED',
    'SERVICE_ASSIGNED',
    'SERVICE_UNASSIGNED',
    'SERVICE_RESCHEDULED',

    -- Project events
    'PROJECT_CREATED',
    'PROJECT_UPDATED',
    'PROJECT_DELETED',
    'PROJECT_STATUS_CHANGED',
    'PROJECT_ASSIGNED',
    'PROJECT_UNASSIGNED',
    'PROJECT_RESCHEDULED',
    'PROJECT_AMOUNT_CHANGED',

    -- Schedule events
    'SCHEDULE_CREATED',
    'SCHEDULE_UPDATED',
    'SCHEDULE_DELETED',
    'SCHEDULE_FREQUENCY_CHANGED',

    -- Session events
    'SERVICE_SESSION_STARTED',
    'SERVICE_SESSION_ENDED',
    'SERVICE_SESSION_REVERTED',
    'PROJECT_SESSION_STARTED',
    'PROJECT_SESSION_ENDED',
    'PROJECT_SESSION_REVERTED',

    -- Session media events
    'SESSION_NOTE_CREATED',
    'SESSION_NOTE_UPDATED',
    'SESSION_NOTE_DELETED',
    'SESSION_IMAGE_UPLOADED',
    'SESSION_IMAGE_UPDATED',
    'SESSION_IMAGE_DELETED',
    'SESSION_VIDEO_UPLOADED',
    'SESSION_VIDEO_UPDATED',
    'SESSION_VIDEO_DELETED',
    'SESSION_MEDIA_INTERNAL_FLAGGED',

    -- Task events
    'SERVICE_TASK_COMPLETED',
    'SERVICE_TASK_UNCOMPLETED',
    'PROJECT_TASK_COMPLETED',
    'PROJECT_TASK_UNCOMPLETED',

    -- Service scope events
    'SERVICE_SCOPE_CREATED',
    'SERVICE_SCOPE_UPDATED',
    'SERVICE_SCOPE_DELETED',
    'SERVICE_SCOPE_ACTIVATED',
    'SERVICE_SCOPE_DEACTIVATED',
    'SERVICE_SCOPE_AREA_CREATED',
    'SERVICE_SCOPE_AREA_UPDATED',
    'SERVICE_SCOPE_AREA_DELETED',
    'SERVICE_SCOPE_TASK_CREATED',
    'SERVICE_SCOPE_TASK_UPDATED',
    'SERVICE_SCOPE_TASK_DELETED',

    -- Project scope events
    'PROJECT_SCOPE_CREATED',
    'PROJECT_SCOPE_UPDATED',
    'PROJECT_SCOPE_DELETED',
    'PROJECT_SCOPE_ACTIVATED',
    'PROJECT_SCOPE_DEACTIVATED',
    'PROJECT_SCOPE_CATEGORY_CREATED',
    'PROJECT_SCOPE_CATEGORY_UPDATED',
    'PROJECT_SCOPE_CATEGORY_DELETED',
    'PROJECT_SCOPE_TASK_CREATED',
    'PROJECT_SCOPE_TASK_UPDATED',
    'PROJECT_SCOPE_TASK_DELETED',

    -- Scope template events
    'SCOPE_TEMPLATE_CREATED',
    'SCOPE_TEMPLATE_UPDATED',
    'SCOPE_TEMPLATE_DELETED',
    'SCOPE_TEMPLATE_ACTIVATED',
    'SCOPE_TEMPLATE_DEACTIVATED',
    'SCOPE_TEMPLATE_INSTANTIATED',

    -- Profile events
    'TEAM_PROFILE_CREATED',
    'TEAM_PROFILE_UPDATED',
    'TEAM_PROFILE_DELETED',
    'TEAM_PROFILE_ROLE_CHANGED',
    'TEAM_PROFILE_STATUS_CHANGED',
    'CUSTOMER_PROFILE_CREATED',
    'CUSTOMER_PROFILE_UPDATED',
    'CUSTOMER_PROFILE_DELETED',
    'CUSTOMER_PROFILE_STATUS_CHANGED',
    'CUSTOMER_PROFILE_ACCESS_GRANTED',
    'CUSTOMER_PROFILE_ACCESS_REVOKED',

    -- Financial events
    'LABOR_CREATED',
    'LABOR_UPDATED',
    'LABOR_DELETED',
    'LABOR_RATE_CHANGED',
    'REVENUE_CREATED',
    'REVENUE_UPDATED',
    'REVENUE_DELETED',
    'REVENUE_AMOUNT_CHANGED',
    'INVOICE_CREATED',
    'INVOICE_UPDATED',
    'INVOICE_DELETED',
    'INVOICE_STATUS_CHANGED',
    'INVOICE_SENT',
    'INVOICE_PAID',
    'INVOICE_OVERDUE',

    -- Report events
    'REPORT_CREATED',
    'REPORT_UPDATED',
    'REPORT_DELETED',
    'REPORT_SUBMITTED',
    'REPORT_APPROVED',

    -- Conversation events
    'CONVERSATION_CREATED',
    'CONVERSATION_UPDATED',
    'CONVERSATION_ARCHIVED',
    'CONVERSATION_UNARCHIVED',
    'CONVERSATION_PARTICIPANT_ADDED',
    'CONVERSATION_PARTICIPANT_REMOVED',

    -- Message events
    'MESSAGE_SENT',
    'MESSAGE_UPDATED',
    'MESSAGE_DELETED',
    'MESSAGE_READ',

    -- Notification events
    'NOTIFICATION_RULE_CREATED',
    'NOTIFICATION_RULE_UPDATED',
    'NOTIFICATION_RULE_DELETED',
    'NOTIFICATION_RULE_ACTIVATED',
    'NOTIFICATION_RULE_DEACTIVATED',
    'NOTIFICATION_CREATED',
    'NOTIFICATION_SENT',
    'NOTIFICATION_READ',
    'NOTIFICATION_FAILED',
    'NOTIFICATION_DELIVERY_ATTEMPTED',
    'NOTIFICATION_DELIVERY_SUCCEEDED',
    'NOTIFICATION_DELIVERY_FAILED',

    -- System events
    'SYSTEM_STARTUP',
    'SYSTEM_SHUTDOWN',
    'MONITORING_TASK_RUN',
    'MONITORING_ALERT_TRIGGERED',
    'BACKGROUND_JOB_STARTED',
    'BACKGROUND_JOB_COMPLETED',
    'BACKGROUND_JOB_FAILED'
);
