CREATE TABLE IF NOT EXISTS vendor_categories (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), name varchar(160) UNIQUE NOT NULL, description text, status boolean NOT NULL DEFAULT true, created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS currencies (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), code varchar(10) UNIQUE NOT NULL, name varchar(100) NOT NULL, symbol varchar(10), status boolean NOT NULL DEFAULT true);
CREATE TABLE IF NOT EXISTS payment_terms (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), name varchar(160) UNIQUE NOT NULL, days integer NOT NULL DEFAULT 0, description text, status boolean NOT NULL DEFAULT true);
CREATE TABLE IF NOT EXISTS industry_sectors (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), name varchar(160) UNIQUE NOT NULL, description text, status boolean NOT NULL DEFAULT true);
CREATE TABLE IF NOT EXISTS units (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), code varchar(50) UNIQUE NOT NULL, name varchar(120) NOT NULL, description text, status boolean NOT NULL DEFAULT true);
CREATE TABLE IF NOT EXISTS validity_periods (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), label varchar(120) NOT NULL, days integer NOT NULL, status boolean NOT NULL DEFAULT true);
CREATE TABLE IF NOT EXISTS terms_conditions (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), title varchar(255) NOT NULL, body text NOT NULL, status boolean NOT NULL DEFAULT true, updated_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS rate_categories (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), name varchar(160) UNIQUE NOT NULL, category_type varchar(80) NOT NULL, description text, status boolean NOT NULL DEFAULT true);
CREATE TABLE IF NOT EXISTS market_rate_items (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), item_code varchar(80) UNIQUE NOT NULL, module_name varchar(120), category_id uuid REFERENCES rate_categories(id), unit_id uuid REFERENCES units(id), description text NOT NULL, standard_rate numeric(18,2) NOT NULL CHECK(standard_rate>=0), effective_date date NOT NULL, active boolean NOT NULL DEFAULT true, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS rate_history (id bigserial PRIMARY KEY, rate_item_id uuid NOT NULL REFERENCES market_rate_items(id), previous_rate numeric(18,2), new_rate numeric(18,2) NOT NULL, changed_by uuid REFERENCES users(id), changed_at timestamptz NOT NULL DEFAULT now());
