-- Search engine domain crawl status tracking
CREATE TABLE IF NOT EXISTS domain_crawl_status (
    domain_id INT PRIMARY KEY,
    last_crawled_at TIMESTAMP,
    next_crawl_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    crawl_status VARCHAR(20) DEFAULT 'pending',
    error_message TEXT,
    pages_found INTEGER DEFAULT 0,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE
);

CREATE INDEX idx_domain_crawl_status_next_crawl ON domain_crawl_status(next_crawl_at);
CREATE INDEX idx_domain_crawl_status_status ON domain_crawl_status(crawl_status);

-- Function to update the updated_at column
-- MySQL trigger to update updated_at column - simplified without DELIMITER
CREATE TRIGGER update_domain_crawl_status_updated_at
BEFORE UPDATE ON domain_crawl_status
FOR EACH ROW
    SET NEW.updated_at = CURRENT_TIMESTAMP;