#!/usr/bin/env node

const { Client } = require("pg");

const CREATE_DB = `
CREATE DATABASE inventory_app;
`;

const SQL = `

CREATE TABLE IF NOT EXISTS store (
  store_id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
  name varchar(80) NOT NULL UNIQUE
);

CREATE TABLE IF NOT EXISTS category (
  category_id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
  name varchar(40) NOT NULL UNIQUE
);

CREATE TABLE IF NOT EXISTS items (
  id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
  name VARCHAR(80) NOT NULL,
  price INTEGER,
  qty INTEGER,
  store_id INTEGER REFERENCES store (store_id),
  category_id INTEGER REFERENCES category (category_id)
);
`;

const POPULATE = `
INSERT INTO category (name) 
VALUES ('grocery'), ('clothing'), ('household'), ('cleaning'), ('misc');

INSERT INTO store (name)
VALUES ('target'), ('wegmans'), ('giant');

INSERT INTO items (name, price, qty, store_id, category_id)
VALUES ('paper towels', '399', '3', '2','3' ); 
`;

async function main() {
  console.log("DB Creation script");

  const client = new Client({
    host: "smig-ca05.lab.smig.tech",
    user: "odin",
    database: "odin",
    password: "odinproject",
    port: 32343,
  });

  try {
    await client.connect();
    await client.query(POPULATE);
    await client.end();
  } catch (e) {
    console.log(`ERROR: ${e}`);
    process.exit(1);
  }
  console.log("Done");
}

main();
