-- +goose Up
-- +goose StatementBegin
DROP TABLE IF EXISTS "heartbeat";
DROP SEQUENCE IF EXISTS heartbeat_id_seq;
CREATE SEQUENCE heartbeat_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;

CREATE TABLE "public"."heartbeat"
(
    "id"         integer DEFAULT nextval('heartbeat_id_seq') NOT NULL,
    "user"       integer                                     NOT NULL,
    "created_at" timestamptz                                 NOT NULL,
    "ip_addr"    text                                        NOT NULL,
    "auth_token" text                                        NOT NULL,
    CONSTRAINT "heartbeat_pkey" PRIMARY KEY ("id")
) WITH (oids = false);

CREATE INDEX "heartbeats_user_idx" ON "public"."heartbeat" USING btree ("user");


DROP TABLE IF EXISTS "organization";
DROP SEQUENCE IF EXISTS organization_id_seq;
CREATE SEQUENCE organization_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;

CREATE TABLE "public"."organization"
(
    "id"          integer          DEFAULT nextval('organization_id_seq') NOT NULL,
    "name"        text        NOT NULL,
    "owner_name"  text        NOT NULL,
    "owner_phone" text        NOT NULL,
    "owner_email" text        NOT NULL,
    "created_at"  timestamptz NOT NULL,
    "deleted_at"  timestamptz NULL DEFAULT NULL,
    "authorized"  integer     NOT NULL,
    "auth_token"  text        NOT NULL,
    CONSTRAINT "organization_name_key" UNIQUE ("name"),
    CONSTRAINT "organization_pkey" PRIMARY KEY ("id")
) WITH (oids = false);

CREATE INDEX "organizations_name_idx" ON "public"."organization" USING btree ("name");

CREATE INDEX "organizations_owner_email_idx" ON "public"."organization" USING btree ("owner_email");

CREATE INDEX "organizations_owner_phone_idx" ON "public"."organization" USING btree ("owner_phone");

DROP TABLE IF EXISTS "user";
DROP SEQUENCE IF EXISTS user_id_seq;
CREATE SEQUENCE user_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;

CREATE TABLE "public"."user"
(
    "id"           integer DEFAULT nextval('user_id_seq') NOT NULL,
    "username"     text                                   NOT NULL,
    "password"     text                                   NOT NULL,
    "name"         text                                   NOT NULL,
    "email"        text                                   NOT NULL,
    "auth_token"   text                                   NOT NULL,
    "authorized"   integer                                NOT NULL,
    "admin"        integer                                NOT NULL,
    "organization" integer                                NOT NULL,
    CONSTRAINT "user_email_key" UNIQUE ("email"),
    CONSTRAINT "user_pkey" PRIMARY KEY ("id"),
    CONSTRAINT "user_username_key" UNIQUE ("username")
) WITH (oids = false);

CREATE INDEX "user_email_idx" ON "public"."user" USING btree ("email");

CREATE INDEX "user_username_idx" ON "public"."user" USING btree ("username");


ALTER TABLE ONLY "public"."heartbeat"
    ADD CONSTRAINT "heartbeat_user_fkey" FOREIGN KEY ("user") REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE;

ALTER TABLE ONLY "public"."user"
    ADD CONSTRAINT "user_organization_fkey" FOREIGN KEY (organization) REFERENCES organization (id) ON DELETE CASCADE NOT DEFERRABLE;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
-- +goose StatementEnd
