-- USERS
CREATE TABLE users (
  id BIGSERIAL PRIMARY KEY,
  id_user BIGINT NOT NULL,
  name VARCHAR NOT NULL,
  email VARCHAR NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ
);

CREATE UNIQUE INDEX users_id_user_unique ON users (id_user) WHERE deleted_at IS NULL;
CREATE UNIQUE INDEX users_email_unique ON users (email) WHERE deleted_at IS NULL;

-- FLAGS
CREATE TABLE flags (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  flagable_id BIGINT NOT NULL,
  flagable_type VARCHAR(50) NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ
);

-- PRODUCT CATEGORIES
CREATE TABLE product_categories (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  code VARCHAR(3) NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);
CREATE UNIQUE INDEX product_categories_code_unique ON product_categories (code) WHERE deleted_at IS NULL;

-- UOM
CREATE TABLE uom (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

-- PRODUCTS
CREATE TABLE products (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  brand VARCHAR NOT NULL,
  sku VARCHAR(100),
  uom_id BIGINT NOT NULL REFERENCES uom(id),
  product_category_id BIGINT NOT NULL REFERENCES product_categories(id),
  product_price NUMERIC(15,2) NOT NULL,
  selling_price NUMERIC(15,2),
  tax NUMERIC(15,2),
  expiry_period INT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);
CREATE UNIQUE INDEX products_sku_unique ON products (sku) WHERE deleted_at IS NULL;

-- BANKS
CREATE TABLE banks (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  alias VARCHAR(5) NOT NULL,
  owner VARCHAR,
  account_number VARCHAR(50) NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

-- AREAS
CREATE TABLE areas (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

-- LOCATIONS
CREATE TABLE locations (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  address TEXT NOT NULL,
  area_id BIGINT NOT NULL REFERENCES areas(id),
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

-- KANDANG
CREATE TABLE kandangs (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR(191) NOT NULL,
  location_id BIGINT NOT NULL REFERENCES locations(id),
  pic_id BIGINT NOT NULL REFERENCES users(id),
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

-- WAREHOUSES
CREATE TABLE warehouses (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  type VARCHAR(50) NOT NULL,
  location_id BIGINT NOT NULL REFERENCES locations(id),
  kandang_id BIGINT REFERENCES kandangs(id),
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

-- CUSTOMERS
CREATE TABLE customers (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  pic_id BIGINT REFERENCES users(id),
  type VARCHAR(50) NOT NULL,
  address TEXT NOT NULL,
  phone VARCHAR(20) NOT NULL,
  email VARCHAR NOT NULL,
  account_number VARCHAR(50) NOT NULL,
  balance NUMERIC(15,2) DEFAULT 0,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

-- NONSTOCK
CREATE TABLE nonstocks (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  uom_id BIGINT NOT NULL REFERENCES uom(id),
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

-- FCR
CREATE TABLE fcr (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

CREATE TABLE fcr_standards (
  id BIGSERIAL PRIMARY KEY,
  fcr_id BIGINT NOT NULL REFERENCES fcr(id) ON DELETE CASCADE ON UPDATE CASCADE,
  weight NUMERIC(15,2) NOT NULL,
  fcr_number NUMERIC(15,2) NOT NULL,
  mortality NUMERIC(15,2) NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ
);

-- SUPPLIERS
CREATE TABLE suppliers (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  alias VARCHAR(5) NOT NULL,
  pic VARCHAR NOT NULL,
  type VARCHAR(50) NOT NULL,
  hatchery VARCHAR,
  phone VARCHAR(20) NOT NULL,
  email VARCHAR NOT NULL,
  address TEXT NOT NULL,
  npwp VARCHAR(50),
  account_number VARCHAR(50),
  balance NUMERIC(15,2) DEFAULT 0,
  due_date INT NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);

-- PROJECTS
CREATE TABLE projects (
  id BIGSERIAL PRIMARY KEY,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  deleted_at TIMESTAMPTZ,
  created_by BIGINT NOT NULL REFERENCES users(id)
);
