-- Add certificate challenges table for CA functionality
CREATE TABLE IF NOT EXISTS certificate_challenges (
    id INT AUTO_INCREMENT PRIMARY KEY,
    token VARCHAR(255) UNIQUE NOT NULL,
    domain VARCHAR(255) NOT NULL,
    challenge_type VARCHAR(20) NOT NULL,
    verification_data VARCHAR(500) NOT NULL,
    status VARCHAR(20) DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP NOT NULL
);

CREATE INDEX idx_certificate_challenges_token ON certificate_challenges(token);
CREATE INDEX idx_certificate_challenges_domain ON certificate_challenges(domain);
CREATE INDEX idx_certificate_challenges_expires_at ON certificate_challenges(expires_at);

-- Add table to store issued certificates
CREATE TABLE IF NOT EXISTS issued_certificates (
    id INT AUTO_INCREMENT PRIMARY KEY,
    domain VARCHAR(255) NOT NULL,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    certificate_pem TEXT NOT NULL,
    private_key_pem TEXT NOT NULL,
    issued_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP NOT NULL,
    revoked_at TIMESTAMP,
    serial_number VARCHAR(255) UNIQUE NOT NULL
);

CREATE INDEX idx_issued_certificates_domain ON issued_certificates(domain);
CREATE INDEX idx_issued_certificates_user_id ON issued_certificates(user_id);
CREATE INDEX idx_issued_certificates_serial ON issued_certificates(serial_number);
CREATE INDEX idx_issued_certificates_expires_at ON issued_certificates(expires_at);