-- export forma_db='{
--     "host":"127.0.0.1",
--     "port":5432,
--     "database":"forma",
--     "user":"postgres",
--     "password":"Shala55951254"
-- }';

-- psql -h 127.0.0.1 -U postgres -a -f conf.sql

DROP DATABASE IF EXISTS formha;

CREATE DATABASE formha;

\c formha;


CREATE TABLE contact (
  id SERIAL PRIMARY KEY,
  full_date_time TIMESTAMP WITH TIME ZONE,
  nombre VARCHAR(50),
  apellido VARCHAR(100),
  email VARCHAR(150),
  estado VARCHAR(50),
  num_tel VARCHAR(20), 
  size_co VARCHAR(40),
  rol_contacto VARCHAR(50),
  industry_type VARCHAR(40),
  tipo_req VARCHAR(255),
  status VARCHAR(50)
);

CREATE TABLE users(
  id VARCHAR(25),
  nombre VARCHAR(50),
  apellido VARCHAR(100),
  genero VARCHAR(2),
  email VARCHAR(150), 
  pswd VARCHAR(100), 
  lst_conn TIMESTAMP WITH TIME ZONE,
  is_admin boolean,
  is_pswd_reseted boolean,
  is_contact_noti boolean
);


INSERT INTO users (id, nombre, apellido, genero, email, pswd, is_admin, is_contact_noti) VALUES
('4HlOjqJ6jLISxNQIbs2Hzz', 'David', 'Itehua Xalamihua', 'M', 'davidix1991@gmail.com', '$2b$12$dbJWK5mv89PszxPeXlql5Otd8vv7kz6M44JnKZcrwJdKoovayiqEm', false, true),
('4FyJhu54R6ARH2FlmroTxl', 'David', 'Itehua Xalamihua', 'M', 'davicho1991@live.com', '$2b$12$dbJWK5mv89PszxPeXlql5Otd8vv7kz6M44JnKZcrwJdKoovayiqEm', true, true);


CREATE TABLE posts (
  id SERIAL PRIMARY KEY,
  id_usr VARCHAR(25),
  created_at TIMESTAMP WITH TIME ZONE,
  updated_at TIMESTAMP WITH TIME ZONE,
  title VARCHAR(150) NOT NULL, 
  body TEXT NOT NULL,
  body_no_img TEXT NOT NULL,
  lista_imagenes JSONB
);

CREATE TABLE posts_visited(
  id_post INT,
  viewed TIMESTAMP WITH TIME ZONE
);


CREATE TABLE visited_from (
    id SERIAL PRIMARY KEY,
    post_id INT NOT NULL,
    source_name VARCHAR(50),
    visit_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);


-- cargar a la base de datos de producción
CREATE TABLE carousel (
  id SERIAL PRIMARY KEY,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  img_name VARCHAR(150) NOT NULL, 
  bg_color VARCHAR(40) NOT NULL,
  txt_color VARCHAR(40) NOT NULL,
  txt VARCHAR(400) NOT NULL
);

ALTER DATABASE formha SET timezone TO 'America/Mexico_City';


SELECT 
  CASE source_name
    WHEN 'li' THEN 'LinkedIn'
    WHEN 'wa' THEN 'WhatsApp'
    WHEN 'fb' THEN 'Facebook'
    WHEN 'x' THEN 'X'
    ELSE 'Otro'
  END AS nombre_legible,
  COUNT(source_name) AS total
FROM visited_from
GROUP BY source_name;

-- select * from visited_from;