CREATE TABLE vhffs_mxdomain
(
    mxdomain_id integer,
    domain varchar,
    boxes_path varchar NOT NULL,
    catchall varchar NOT NULL,
    object_id int4 NOT NULL,
    CONSTRAINT vhffs_mxdomain_pkey PRIMARY KEY (mxdomain_id)
) WITH OIDS;

CREATE TABLE vhffs_boxes
(
    domain varchar,
    local_part varchar,
    domain_hash varchar NOT NULL,
    mbox_name varchar NOT NULL,
    password varchar NOT NULL,
    nospam boolean,
    novirus boolean,
    allowpop boolean NOT NULL,
    allowimap boolean NOT NULL,
    state int4 NOT NULL,
    CONSTRAINT vhffs_boxes_pkey PRIMARY KEY (domain,local_part)
) WITH OIDS;

CREATE TABLE vhffs_forward
(
    domain varchar NOT NULL,
    local_part varchar NOT NULL,
    remote_name varchar NOT NULL,
    password varchar NOT NULL,
    CONSTRAINT vhffs_forward_pkey PRIMARY KEY (domain , local_part)
) WITH OIDS;

CREATE TABLE vhffs_ml
(
    ml_id integer,
    local_part varchar(256) NOT NULL,
    domain varchar(256) NOT NULL,
    prefix varchar(32),
    object_id int4 NOT NULL,
    sub_ctrl int4 NOT NULL,
    post_ctrl int4 NOT NULL,
    reply_to boolean,
    open_archive boolean,
    signature varchar(250),
    CONSTRAINT vhffs_ml_pkey PRIMARY KEY (ml_id)
) WITH OIDS;


CREATE TABLE vhffs_ml_subscribers
(
    sub_id integer,
    member varchar(256) NOT NULL,
    perm int4 NOT NULL,
    hash varchar,
    ml_id int4 NOT NULL,
    language varchar(16),
    CONSTRAINT vhffs_ml_subscribers_pkey PRIMARY KEY (sub_id)
) WITH OIDS;

CREATE TABLE vhffs_object
(
    object_id integer,
    owner_uid int4,
    owner_gid int4,
    date_creation int8,
    state int4 NOT NULL,
    description TEXT,
    refuse_reason TEXT,
    type int4 NOT NULL DEFAULT 0,
    CONSTRAINT vhffs_object_pkey PRIMARY KEY (object_id)
) WITH OIDS;

-- Some indices, you could add more
CREATE INDEX idx_vhffs_ml_domain ON vhffs_ml(domain);
CREATE INDEX idx_vhffs_object_state ON vhffs_object(state);
CREATE INDEX idx_vhffs_boxes_state ON vhffs_boxes(state);
CREATE INDEX idx_vhffs_ml_open_archive ON vhffs_ml(open_archive);
CREATE INDEX idx_vhffs_object_owner_uid ON vhffs_object(owner_uid);
CREATE INDEX idx_vhffs_object_owner_gid ON vhffs_object(owner_gid);
CREATE INDEX idx_vhffs_object_type ON vhffs_object(type);
CREATE INDEX idx_vhffs_ml_object_id ON vhffs_ml(object_id);
CREATE INDEX idx_vhffs_mxdomain_object_id ON vhffs_mxdomain(object_id);
CREATE INDEX idx_vhffs_object_date_creation ON vhffs_object(date_creation);

-- Foreign key constraints
ALTER TABLE vhffs_boxes ADD CONSTRAINT fk_vhffs_boxes_vhffs_mxdomain FOREIGN KEY (domain) REFERENCES vhffs_mxdomain(domain) ON DELETE CASCADE;
ALTER TABLE vhffs_forward ADD CONSTRAINT fk_vhffs_forward_vhffs_mxdomain FOREIGN KEY (domain) REFERENCES vhffs_mxdomain(domain) ON DELETE CASCADE;
ALTER TABLE vhffs_ml ADD CONSTRAINT fk_vhffs_ml_vhffs_object FOREIGN KEY (object_id) REFERENCES vhffs_object(object_id) ON DELETE CASCADE;
ALTER TABLE vhffs_ml ADD CONSTRAINT fk_vhffs_ml_vhffs_mxdomain FOREIGN KEY (domain) REFERENCES vhffs_mxdomain(domain) ON DELETE CASCADE;
ALTER TABLE vhffs_ml_subscribers ADD CONSTRAINT fk_vhffs_ml_subscribers_vhffs_ml FOREIGN KEY (ml_id) REFERENCES vhffs_ml(ml_id) ON DELETE CASCADE;
ALTER TABLE vhffs_mxdomain ADD CONSTRAINT fk_vhffs_mxdomain_vhffs_object FOREIGN KEY (object_id) REFERENCES vhffs_object(object_id) ON DELETE CASCADE;

-- unique constraints (implicitely create indices)
ALTER TABLE vhffs_mxdomain ADD CONSTRAINT vhffs_mxdomain_unique_domainname UNIQUE (domain);
ALTER TABLE vhffs_ml ADD CONSTRAINT vhffs_ml_unique_address UNIQUE (local_part, domain);
ALTER TABLE vhffs_ml_subscribers ADD CONSTRAINT vhffs_ml_subscribers_member_list UNIQUE (ml_id, member);

