CREATE TABLE mail_signatures (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    name TEXT NOT NULL,
    html TEXT NOT NULL DEFAULT '',
    sort_order INT NOT NULL DEFAULT 0,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_mail_signatures_user ON mail_signatures(user_id);

ALTER TABLE mail_identities
    ADD COLUMN default_signature_id UUID REFERENCES mail_signatures(id) ON DELETE SET NULL;

CREATE INDEX idx_mail_identities_default_signature ON mail_identities(default_signature_id);
