-- Migration 007: Visma eEkonomi features
-- Allt ett företag behöver

-- 1. LÖN (Payroll)
CREATE TABLE IF NOT EXISTS boc_payroll (
    id SERIAL PRIMARY KEY,
    employee_id TEXT NOT NULL,
    period TEXT NOT NULL,
    gross_salary NUMERIC(12,2) NOT NULL,
    tax_deduction NUMERIC(12,2) NOT NULL,
    employer_contribution NUMERIC(12,2) NOT NULL,
    net_salary NUMERIC(12,2) NOT NULL,
    payment_date DATE,
    status TEXT DEFAULT 'draft',
    created_at TIMESTAMP DEFAULT NOW()
);

-- 2. TID (Time tracking)
CREATE TABLE IF NOT EXISTS boc_time_entries (
    id SERIAL PRIMARY KEY,
    employee_id TEXT NOT NULL,
    date DATE NOT NULL,
    hours NUMERIC(4,2) NOT NULL,
    project_id TEXT,
    description TEXT,
    billable BOOLEAN DEFAULT false,
    created_at TIMESTAMP DEFAULT NOW()
);

-- 3. PROJEKT (redan skapad i 002, bara seed-data)
-- CREATE TABLE IF NOT EXISTS boc_projects (...); -- redan finns

-- 4. LAGER (Inventory)
CREATE TABLE IF NOT EXISTS boc_inventory (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    sku TEXT UNIQUE,
    quantity INTEGER DEFAULT 0,
    unit_cost NUMERIC(12,2),
    unit_price NUMERIC(12,2),
    category TEXT,
    created_at TIMESTAMP DEFAULT NOW()
);

-- 5. LEVERANTÖRER (Suppliers)
CREATE TABLE IF NOT EXISTS boc_suppliers (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT,
    phone TEXT,
    org_number TEXT,
    address TEXT,
    payment_terms TEXT,
    status TEXT DEFAULT 'active',
    created_at TIMESTAMP DEFAULT NOW()
);

-- 6. INKÖP (Purchases)
CREATE TABLE IF NOT EXISTS boc_purchases (
    id TEXT PRIMARY KEY,
    supplier_id TEXT,
    amount NUMERIC(12,2) NOT NULL,
    currency TEXT DEFAULT 'SEK',
    status TEXT DEFAULT 'draft',
    due_date DATE,
    created_at TIMESTAMP DEFAULT NOW()
);

-- 7. RAPPORTER (Reports)
CREATE TABLE IF NOT EXISTS boc_reports (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    type TEXT NOT NULL,
    period TEXT,
    data JSONB,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Seed data
-- INSERT INTO boc_projects (...) -- redan finns data

-- INSERT INTO boc_inventory (...) -- redan finns data
-- INSERT INTO boc_suppliers (...) -- redan finns data
-- INSERT INTO boc_purchases (...) -- redan finns data
