CREATE TABLE public.m_shift (
    id SERIAL,
    label VARCHAR(128) NOT NULL,
    flex_time_minute INTEGER NOT NULL DEFAULT 30,
    start_time TIME WITH TIME ZONE NOT NULL,
    end_time TIME WITH TIME ZONE NOT NULL,
    is_non_shift BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMP WITH TIME ZONE,
    created_by VARCHAR(128),
    updated_at TIMESTAMP WITH TIME ZONE,
    updated_by VARCHAR(128),
    UNIQUE(label),
    PRIMARY KEY(id)
);

ALTER TABLE public.m_users ADD PRIMARY KEY(id);
ALTER TABLE public.m_proyek ADD PRIMARY KEY(id);
ALTER TABLE public.m_company ADD PRIMARY KEY(id);

-- TEMPORARY
-- ALTER TABLE m_shift RENAME COLUMN is_default TO is_non_shift;

CREATE TABLE public.t_users_monthly_shift (
    id BIGSERIAL,
    company_id BIGINT,
    project_id BIGINT,
    start_at TIMESTAMP WITH TIME ZONE NOT NULL,
    end_at TIMESTAMP WITH TIME ZONE,
    user_id BIGINT NOT NULL,
    remarks VARCHAR(255),
    schedules JSON,
    created_at TIMESTAMP WITH TIME ZONE,
    created_by VARCHAR(128),
    updated_at TIMESTAMP WITH TIME ZONE,
    updated_by VARCHAR(128),
    FOREIGN KEY(project_id) REFERENCES m_proyek(id),
    FOREIGN KEY(user_id) REFERENCES m_users(id),
    FOREIGN KEY(company_id) REFERENCES m_company(id),
    PRIMARY KEY(id)
);

CREATE TABLE public.t_users_shift (
    id BIGSERIAL,
    project_id BIGINT,
    user_id BIGINT NOT NULL,
    from_date TIMESTAMP WITH TIME ZONE NOT NULL,
    to_date TIMESTAMP WITH TIME ZONE,
    remarks VARCHAR(255),
    mon_shift_id INTEGER,
    tue_shift_id INTEGER,
    wed_shift_id INTEGER,
    thu_shift_id INTEGER,
    fri_shift_id INTEGER,
    sat_shift_id INTEGER,
    sun_shift_id INTEGER,
    created_at TIMESTAMP WITH TIME ZONE,
    created_by VARCHAR(128),
    updated_at TIMESTAMP WITH TIME ZONE,
    updated_by VARCHAR(128),
    FOREIGN KEY(project_id) REFERENCES m_proyek(id),
    FOREIGN KEY(user_id) REFERENCES m_users(id),
    FOREIGN KEY(mon_shift_id) REFERENCES m_shift(id),
    FOREIGN KEY(tue_shift_id) REFERENCES m_shift(id),
    FOREIGN KEY(wed_shift_id) REFERENCES m_shift(id),
    FOREIGN KEY(thu_shift_id) REFERENCES m_shift(id),
    FOREIGN KEY(fri_shift_id) REFERENCES m_shift(id),
    FOREIGN KEY(sat_shift_id) REFERENCES m_shift(id),
    FOREIGN KEY(sun_shift_id) REFERENCES m_shift(id),
    PRIMARY KEY(id)
);

--- SAMPLE DATA
INSERT INTO m_shift(label,flex_time_minute, start_time, end_time, is_non_shift, created_at, created_by) 
VALUES('S0', 30, '07:30:00', '15:30:00', TRUE, CURRENT_TIMESTAMP, 'System');

INSERT INTO t_users_shift(user_id, from_date, 
    mon_shift_id, tue_shift_id, wed_shift_id, thu_shift_id, fri_shift_id, sat_shift_id, sun_shift_id,
    created_at, created_by)
VALUES(1, CURRENT_TIMESTAMP,
    1, 1, 1, 1, 1, NULL, NULL, CURRENT_TIMESTAMP, 'System');