-- Create document sessions table for OnlyOffice integration
CREATE TABLE IF NOT EXISTS document_sessions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    document_id UUID NOT NULL,
    document_key VARCHAR(255) UNIQUE NOT NULL,
    user_id UUID NOT NULL REFERENCES users(id),
    status INTEGER NOT NULL DEFAULT 0,
    is_locked BOOLEAN DEFAULT false,
    locked_by UUID REFERENCES users(id),
    locked_at TIMESTAMP WITHOUT TIME ZONE,
    last_saved_at TIMESTAMP WITHOUT TIME ZONE,
    version INTEGER NOT NULL DEFAULT 1,
    created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Create indexes for document sessions
CREATE INDEX idx_document_sessions_document ON document_sessions(document_id);
CREATE INDEX idx_document_sessions_user ON document_sessions(user_id);
CREATE INDEX idx_document_sessions_status ON document_sessions(status);
CREATE INDEX idx_document_sessions_locked ON document_sessions(is_locked);

-- Create document versions table
CREATE TABLE IF NOT EXISTS document_versions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    document_id UUID NOT NULL,
    version INTEGER NOT NULL,
    file_url TEXT NOT NULL,
    file_size BIGINT NOT NULL,
    changes_url TEXT,
    saved_by UUID NOT NULL REFERENCES users(id),
    saved_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    is_active BOOLEAN DEFAULT false,
    comments TEXT,
    created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Create indexes for document versions
CREATE INDEX idx_document_versions_document ON document_versions(document_id);
CREATE INDEX idx_document_versions_active ON document_versions(is_active);
CREATE UNIQUE INDEX idx_document_versions_active_unique ON document_versions(document_id, is_active) WHERE is_active = true;

-- Create document metadata table
CREATE TABLE IF NOT EXISTS document_metadata (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    document_id UUID UNIQUE NOT NULL,
    document_type VARCHAR(50) NOT NULL,
    reference_id UUID NOT NULL,
    file_name TEXT NOT NULL,
    file_type VARCHAR(50) NOT NULL,
    file_size BIGINT NOT NULL,
    mime_type VARCHAR(255),
    created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Create indexes for document metadata
CREATE INDEX idx_document_metadata_document ON document_metadata(document_id);
CREATE INDEX idx_document_metadata_type ON document_metadata(document_type);
CREATE INDEX idx_document_metadata_reference ON document_metadata(reference_id);

-- Create document errors table for logging
CREATE TABLE IF NOT EXISTS document_errors (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    document_id UUID NOT NULL,
    session_id UUID REFERENCES document_sessions(id),
    error_type VARCHAR(100) NOT NULL,
    error_msg TEXT NOT NULL,
    details JSONB,
    created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Create indexes for document errors
CREATE INDEX idx_document_errors_document ON document_errors(document_id);
CREATE INDEX idx_document_errors_session ON document_errors(session_id);
CREATE INDEX idx_document_errors_type ON document_errors(error_type);

-- Add trigger to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

CREATE TRIGGER update_document_sessions_updated_at BEFORE UPDATE ON document_sessions
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER update_document_metadata_updated_at BEFORE UPDATE ON document_metadata
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();