
-- Right

create sequence right_seq start 25;

create table x_right (
	right_id 			bigint 			PRIMARY KEY,
	code			    varchar(50) 	NOT NULL,
	name 	     		varchar(150) 	NOT NULL,
	jpa_active          boolean        	NOT NULL,
    jpa_created         timestamp      	NOT NULL,
    jpa_updated         timestamp      	NOT NULL,
    jpa_version         integer        	NOT NULL,
	CONSTRAINT unq_x_right_code UNIQUE(code)
);


-- user

create sequence user_seq start 25;

create table x_user (
	user_id				bigint 			PRIMARY KEY,
	username			varchar(150) 	NOT NULL,
	password 	    	varchar(150) 	NOT NULL,
	salt	 	   	 	varchar(150) 	NOT NULL,
	title				varchar(15) 	,
	firstname			varchar(150) 	NOT NULL,
	lastname			varchar(150) 	NOT NULL,
	email				varchar(150) 	NOT NULL,
	jpa_active          boolean        	NOT NULL,
    jpa_created         timestamp      	NOT NULL,
    jpa_updated         timestamp      	NOT NULL,
    jpa_version         integer        	NOT NULL,
	CONSTRAINT unq_x_user_username UNIQUE(username)
);

create table user_to_right (
	user_id_fk 		bigint 			REFERENCES x_user,
	right_id_fk 	bigint 	        REFERENCES x_right,
	PRIMARY KEY(user_id_fk, right_id_fk) 
);


-- jwt_refresh_token

CREATE SEQUENCE jwt_refresh_token_seq START 1 INCREMENT 1;

CREATE TABLE jwt_refresh_token (
    jwt_refresh_token_id    BIGINT          PRIMARY KEY,
    token_hash              VARCHAR(255)    NOT NULL,  -- SHA-256 hash of refresh token
    device_info             VARCHAR(255),              -- Browser/device identifier
    ip_address              VARCHAR(45),
    issued_at               TIMESTAMP       NOT NULL,
    expires_at              TIMESTAMP       NOT NULL,
    revoked_at              TIMESTAMP,
    last_used_at            TIMESTAMP,
    user_id_fk              BIGINT          NOT NULL REFERENCES x_user(user_id),
    UNIQUE (token_hash)
);

CREATE INDEX idx_jwt_refresh_token_user ON jwt_refresh_token(user_id_fk);
CREATE INDEX idx_jwt_refresh_token_expires ON jwt_refresh_token(expires_at);



-- customer

create sequence customer_seq start 25;

create table customer (
	customer_id			bigint 			PRIMARY KEY,
	customer_number		varchar(150) 	NOT NULL,
	name 	    	    varchar(150) 	NOT NULL,
	jpa_active          boolean        	NOT NULL,
    jpa_created         timestamp      	NOT NULL,
    jpa_updated         timestamp      	NOT NULL,
    jpa_version         integer        	NOT NULL,
	CONSTRAINT unq_customer_number UNIQUE(customer_number)
);


-- picture

create sequence picture_seq start 25;

create table picture (
	picture_id			bigint 			PRIMARY KEY,
	username			varchar(150),
	picture_date        timestamp      	NOT NULL,
	comment 	    	TEXT,
	image 	    	    TEXT,
	jpa_active          boolean        	NOT NULL,
    jpa_created         timestamp      	NOT NULL,
    jpa_updated         timestamp      	NOT NULL,
    jpa_version         integer        	NOT NULL,
    customer_id_fk		bigint			REFERENCES customer
);


-- initial users

insert into x_right (right_id, code, name,jpa_active,jpa_created,jpa_updated,jpa_version) VALUES
  (1,	'ADMIN', 'Admin Right',	true,TIMESTAMP '2026-01-20 10:09:30.009',TIMESTAMP '2026-01-20 10:09:30.009',0),
  (2,	'USER',	 'User Right',  true,TIMESTAMP '2026-01-20 10:09:52.797',TIMESTAMP '2026-01-20 10:09:52.797',0)
  ;
  
-- nvlev4YnTi
-- x1t0e7Pb49
  
INSERT INTO x_user (user_id,username,password,salt,title,firstname,lastname,email,jpa_active,jpa_created,jpa_updated,jpa_version) 
VALUES
  (1,'hartmann','vPsg/G5xQWoJTOA0r9b9HPTEAzMktKg7fKCrnmHYcyQ=', '9bARmw6zzbXPg4qdbj5RAe2OlJ9mz0Lpq3ZKJlg8Iug=','Herr','Hartmann','Admin','admin@heyday.marketing',true,TIMESTAMP '2026-01-20 10:09:52.000',TIMESTAMP '2026-01-20 10:09:52.000',0),
  (2,'adm','eXlSEtLDfqos/w0DqPQiVoJHVEQaqLwD7qeDx74Onmk=','vajK924ZRXNWmt9GkcK/BO/Oc1bYp582MJ47HzsXyzA=','Herr','Hartmann','adm','adm@heyday.marketing',true,TIMESTAMP '2026-01-20 10:09:52.000',TIMESTAMP '2026-01-20 10:09:52.000',0);


INSERT INTO user_to_right (user_id_fk,right_id_fk) 
VALUES
  (1,1),
  (2,2);
