-- Add infrastructure fields to projects table for git/deployment tracking.
-- This enables projects to be associated with git repos, domains, and deployments.

-- Projects table for persistent project management
CREATE TABLE IF NOT EXISTS projects (
    id               TEXT PRIMARY KEY,
    name             TEXT NOT NULL UNIQUE,
    description      TEXT,
    created_at       TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at       TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Git repository fields
ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    git_repo_owner VARCHAR(255);

ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    git_repo_name VARCHAR(255);

ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    git_clone_ssh VARCHAR(512);

ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    git_clone_http VARCHAR(512);

ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    git_html_url VARCHAR(512);

-- Deployment fields
ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    domain VARCHAR(255);

ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    custom_domain VARCHAR(255);

ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    deployment_image VARCHAR(512);

ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    deployment_status VARCHAR(50) DEFAULT 'none';

ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    deployment_replicas INTEGER DEFAULT 1;

ALTER TABLE projects ADD COLUMN IF NOT EXISTS
    deployment_port INTEGER DEFAULT 8080;

-- Index for domain lookups (for routing)
CREATE INDEX IF NOT EXISTS idx_projects_domain ON projects(domain);
CREATE INDEX IF NOT EXISTS idx_projects_custom_domain ON projects(custom_domain);

-- Index for git repo lookups
CREATE INDEX IF NOT EXISTS idx_projects_git_repo ON projects(git_repo_owner, git_repo_name);

-- Update trigger for updated_at
CREATE OR REPLACE FUNCTION update_projects_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS projects_updated_at ON projects;
CREATE TRIGGER projects_updated_at
    BEFORE UPDATE ON projects
    FOR EACH ROW
    EXECUTE FUNCTION update_projects_updated_at();

COMMENT ON TABLE projects IS 'Projects with associated git repos and deployments';
COMMENT ON COLUMN projects.id IS 'Unique project identifier';
COMMENT ON COLUMN projects.name IS 'Human-readable project name (unique)';
COMMENT ON COLUMN projects.description IS 'Optional project description';
COMMENT ON COLUMN projects.git_repo_owner IS 'Git repository owner/org (e.g., threesix)';
COMMENT ON COLUMN projects.git_repo_name IS 'Git repository name';
COMMENT ON COLUMN projects.git_clone_ssh IS 'SSH clone URL (git@git.threesix.ai:owner/name.git)';
COMMENT ON COLUMN projects.git_clone_http IS 'HTTPS clone URL';
COMMENT ON COLUMN projects.git_html_url IS 'Web UI URL for the repository';
COMMENT ON COLUMN projects.domain IS 'Auto-assigned domain (e.g., myproject.threesix.ai)';
COMMENT ON COLUMN projects.custom_domain IS 'User-configured custom domain';
COMMENT ON COLUMN projects.deployment_image IS 'Container image for deployment';
COMMENT ON COLUMN projects.deployment_status IS 'Current deployment status: none, pending, running, failed';
COMMENT ON COLUMN projects.deployment_replicas IS 'Number of deployment replicas';
COMMENT ON COLUMN projects.deployment_port IS 'Container port for the deployment';
