-- Migration: Enhance contact_message table
-- Date: 2026-05-08
-- Description: Add phone, is_read, status, admin_notes columns and rename name to full_name

-- Rename name column to full_name
ALTER TABLE contact_message RENAME COLUMN name TO full_name;

-- Add new columns
ALTER TABLE contact_message ADD COLUMN IF NOT EXISTS phone VARCHAR(50);
ALTER TABLE contact_message ADD COLUMN IF NOT EXISTS is_read BOOLEAN DEFAULT FALSE;
ALTER TABLE contact_message ADD COLUMN IF NOT EXISTS status VARCHAR(20) DEFAULT 'new';
ALTER TABLE contact_message ADD COLUMN IF NOT EXISTS admin_notes TEXT;

-- Update existing records to have default values
UPDATE contact_message SET is_read = FALSE WHERE is_read IS NULL;
UPDATE contact_message SET status = 'new' WHERE status IS NULL;

-- Add constraints to ensure valid status values
ALTER TABLE contact_message ADD CONSTRAINT check_status CHECK (status IN ('new', 'read', 'replied'));

-- Add comments
COMMENT ON COLUMN contact_message.full_name IS 'Customer full name';
COMMENT ON COLUMN contact_message.phone IS 'Customer phone number (optional)';
COMMENT ON COLUMN contact_message.is_read IS 'Whether admin has read this message';
COMMENT ON COLUMN contact_message.status IS 'Message status: new, read, or replied';
COMMENT ON COLUMN contact_message.admin_notes IS 'Internal notes from admin';

-- Create index on status for faster filtering
CREATE INDEX IF NOT EXISTS idx_contact_message_status ON contact_message(status);
CREATE INDEX IF NOT EXISTS idx_contact_message_is_read ON contact_message(is_read);
CREATE INDEX IF NOT EXISTS idx_contact_message_created_at ON contact_message(created_at DESC);
