-- Migration: 003_add_token_prefix
-- Add token prefix field to applications table

-- Add prefix field to applications table
ALTER TABLE applications ADD COLUMN token_prefix VARCHAR(10) DEFAULT '' NOT NULL;

-- Add check constraint to ensure prefix is 2-4 uppercase letters
ALTER TABLE applications ADD CONSTRAINT chk_token_prefix_format 
    CHECK (token_prefix ~ '^[A-Z]{2,4}$' OR token_prefix = '');

-- Create index for prefix field
CREATE INDEX idx_applications_token_prefix ON applications(token_prefix);

-- Update existing applications with empty prefix (they will use the default "kms_" prefix)
-- Applications can later be updated to have custom prefixes

-- Set the internal application prefix to "KMS"
UPDATE applications 
SET token_prefix = 'KMS' 
WHERE app_id = 'internal.api-key-service';