-- Migration to convert webhooks tables to use UUIDs
-- Since the feature was disabled/broken, we drop and recreate to ensure clean state

DROP TABLE IF EXISTS webhook_failures;
DROP TABLE IF EXISTS webhooks;

CREATE TABLE webhooks (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    url TEXT NOT NULL,
    events TEXT[],
    active BOOLEAN DEFAULT true,
    secret TEXT NOT NULL,
    created_at TIMESTAMPTZ,
    updated_at TIMESTAMPTZ
);

CREATE TABLE webhook_failures (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    webhook_id UUID NOT NULL REFERENCES webhooks(id) ON DELETE CASCADE,
    event TEXT NOT NULL,
    error TEXT NOT NULL,
    retries INTEGER DEFAULT 0,
    created_at TIMESTAMPTZ NOT NULL
);

CREATE INDEX idx_webhooks_user_id ON webhooks(user_id);
CREATE INDEX idx_webhook_failures_webhook_id ON webhook_failures(webhook_id);
