ALTER TABLE "profile" DROP CONSTRAINT IF EXISTS "profile_fk0";

ALTER TABLE "profile" DROP CONSTRAINT IF EXISTS "profile_fk1";

ALTER TABLE "venue" DROP CONSTRAINT IF EXISTS "venue_fk0";

ALTER TABLE "billing" DROP CONSTRAINT IF EXISTS "billing_fk0";

DROP TABLE IF EXISTS "logins";

DROP TABLE IF EXISTS "profile";

DROP TABLE IF EXISTS "company";

DROP TABLE IF EXISTS "venue";

DROP TABLE IF EXISTS "billing";

DROP TABLE IF EXISTS "master_beacons";


CREATE TABLE "logins" (
	"id" serial NOT NULL,
	"username" varchar(100) NOT NULL UNIQUE,
	"email" varchar(150) NOT NULL UNIQUE,
	"password_hash" varchar(78) NOT NULL,
	"password_reset_token" varchar(128) UNIQUE,
	"uid" varchar(22) UNIQUE,
	CONSTRAINT logins_pk PRIMARY KEY ("id")
) WITH (
  OIDS=FALSE
);



CREATE TABLE "profile" (
	"id" serial NOT NULL,
	"uid" varchar(22) NOT NULL,
	"forename" varchar(75) NOT NULL,
	"surname" varchar(75) NOT NULL,
	"gender" int NOT NULL,
	"dob" DATE NOT NULL,
	"bio" TEXT NOT NULL,
	"member_of" int,
	CONSTRAINT profile_pk PRIMARY KEY ("id")
) WITH (
  OIDS=FALSE
);



CREATE TABLE "company" (
	"id" serial NOT NULL,
	"company_name" varchar(100) NOT NULL,
	"address1" varchar(150) NOT NULL,
	"address2" varchar(150) NOT NULL,
	"address3" varchar(150) NOT NULL,
	"town" varchar(150) NOT NULL,
	"county" varchar(150) NOT NULL,
	"postcode" varchar(12) NOT NULL,
	"country" int NOT NULL,
	"pcontact" varchar(20) NOT NULL,
	"ocontact" varchar(20) NOT NULL,
	"mobile" varchar(20) NOT NULL,
	"email" varchar(150) NOT NULL,
	CONSTRAINT company_pk PRIMARY KEY ("id")
) WITH (
  OIDS=FALSE
);



CREATE TABLE "venue" (
	"id" serial NOT NULL,
	"venue_name" varchar(100) NOT NULL,
	"address1" varchar(150) NOT NULL,
	"address2" varchar(150) NOT NULL,
	"address3" varchar(150) NOT NULL,
	"town" varchar(150) NOT NULL,
	"county" varchar(150) NOT NULL,
	"postcode" varchar(12) NOT NULL,
	"country" int NOT NULL,
	"pcontact" varchar(20) NOT NULL,
	"ocontact" varchar(20) NOT NULL,
	"mobile" varchar(20) NOT NULL,
	"email" varchar(150) NOT NULL,
	"company_id" int NOT NULL,
	CONSTRAINT venue_pk PRIMARY KEY ("id")
) WITH (
  OIDS=FALSE
);



CREATE TABLE "billing" (
	"id" serial NOT NULL,
	"company_id" int NOT NULL,
	CONSTRAINT billing_pk PRIMARY KEY ("id")
) WITH (
  OIDS=FALSE
);



CREATE TABLE "master_beacons" (
	"id" bigint NOT NULL,
	"uid" uuid NOT NULL,
	CONSTRAINT master_beacons_pk PRIMARY KEY ("id")
) WITH (
  OIDS=FALSE
);




ALTER TABLE "profile" ADD CONSTRAINT "profile_fk0" FOREIGN KEY ("user_id") REFERENCES "logins"("id");
ALTER TABLE "profile" ADD CONSTRAINT "profile_fk1" FOREIGN KEY ("member_of") REFERENCES "company"("id");


ALTER TABLE "venue" ADD CONSTRAINT "venue_fk0" FOREIGN KEY ("company_id") REFERENCES "company"("id");

ALTER TABLE "billing" ADD CONSTRAINT "billing_fk0" FOREIGN KEY ("company_id") REFERENCES "company"("id");

grant connect on database "oBrand" to obrand;
GRANT SELECT, UPDATE, INSERT, DELETE, TRIGGER ON TABLE public.logins TO obrand;
grant select, update on logins_id_seq to obrand;
grant select, update on profile_id_seq to obrand;
