-- Migration V008: Data Deletion Requests Table (GDPR Compliance)
-- Created: 2025-10-03
-- Description: Table to track GDPR data deletion requests and their processing status

-- Create data_deletion_requests table
CREATE TABLE IF NOT EXISTS data_deletion_requests (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,

  -- Request details
  request_type VARCHAR(50) NOT NULL DEFAULT 'full_deletion', -- full_deletion, partial_deletion, anonymization
  reason TEXT,
  requested_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

  -- Processing status
  status VARCHAR(50) NOT NULL DEFAULT 'pending', -- pending, in_progress, completed, failed, cancelled
  scheduled_deletion_date TIMESTAMP, -- When the deletion will be executed
  completed_at TIMESTAMP,

  -- What data to delete (for partial deletions)
  data_types JSONB, -- ['activities', 'photos', 'ai_conversations', etc.]

  -- Processing details
  processor_id UUID REFERENCES users(id), -- Admin/system user who processed the request
  processing_notes TEXT,
  error_message TEXT,

  -- Audit trail
  ip_address VARCHAR(45),
  user_agent TEXT,
  deletion_confirmation_token VARCHAR(255) UNIQUE, -- Token sent via email for confirmation
  confirmed_at TIMESTAMP,

  -- Metadata
  metadata JSONB, -- Additional context (e.g., backup location, retention policy)

  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- Create indexes
CREATE INDEX IF NOT EXISTS idx_data_deletion_requests_user_id
  ON data_deletion_requests(user_id);

CREATE INDEX IF NOT EXISTS idx_data_deletion_requests_status
  ON data_deletion_requests(status);

CREATE INDEX IF NOT EXISTS idx_data_deletion_requests_scheduled
  ON data_deletion_requests(scheduled_deletion_date)
  WHERE status IN ('pending', 'in_progress');

CREATE INDEX IF NOT EXISTS idx_data_deletion_requests_token
  ON data_deletion_requests(deletion_confirmation_token)
  WHERE deletion_confirmation_token IS NOT NULL;

-- Create updated_at trigger
CREATE OR REPLACE FUNCTION update_data_deletion_requests_updated_at()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = CURRENT_TIMESTAMP;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_update_data_deletion_requests_updated_at
  BEFORE UPDATE ON data_deletion_requests
  FOR EACH ROW
  EXECUTE FUNCTION update_data_deletion_requests_updated_at();

-- Add comments for documentation
COMMENT ON TABLE data_deletion_requests IS 'GDPR Article 17 - Right to Erasure: Tracks user data deletion requests';
COMMENT ON COLUMN data_deletion_requests.request_type IS 'Type of deletion: full_deletion (all data), partial_deletion (specific data types), anonymization (pseudonymize data)';
COMMENT ON COLUMN data_deletion_requests.status IS 'Processing status: pending (awaiting confirmation), in_progress (being processed), completed (finished), failed (error), cancelled (user cancelled)';
COMMENT ON COLUMN data_deletion_requests.data_types IS 'JSON array of data types to delete for partial deletions: ["activities", "photos", "ai_conversations", "children", "family"]';
COMMENT ON COLUMN data_deletion_requests.scheduled_deletion_date IS 'Date when deletion will be executed (typically 30 days after request for grace period)';
COMMENT ON COLUMN data_deletion_requests.deletion_confirmation_token IS 'Unique token sent to user email for confirming deletion request';
COMMENT ON COLUMN data_deletion_requests.metadata IS 'Additional metadata: backup location, retention policy exceptions, legal holds';

-- Insert migration record
-- This will be done by the migration runner
