-- Financial management tables

-- Budgets table
CREATE TABLE budgets (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    category VARCHAR(100) NOT NULL,
    yearly_limit DECIMAL(12,2),
    monthly_limit DECIMAL(12,2),
    current_spend DECIMAL(12,2) DEFAULT 0,
    fiscal_year INTEGER NOT NULL,
    start_date TIMESTAMP NOT NULL,
    end_date TIMESTAMP NOT NULL,
    active BOOLEAN DEFAULT true,
    alert_threshold DECIMAL(5,2) DEFAULT 80.00,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INTEGER,
    updated_by INTEGER
);

-- Sponsorships table
CREATE TABLE sponsorships (
    id SERIAL PRIMARY KEY,
    sponsor_name VARCHAR(255) NOT NULL,
    sponsor_logo VARCHAR(500),
    contact_person VARCHAR(255),
    contact_email VARCHAR(255),
    contact_phone VARCHAR(50),
    contract_number VARCHAR(100) UNIQUE,
    contract_type VARCHAR(100),
    total_value DECIMAL(12,2),
    payment_schedule VARCHAR(100) DEFAULT 'Měsíčně',
    currency VARCHAR(3) DEFAULT 'CZK',
    start_date TIMESTAMP NOT NULL,
    end_date TIMESTAMP NOT NULL,
    auto_renewal BOOLEAN DEFAULT false,
    renewal_notice INTEGER DEFAULT 90,
    benefits TEXT,
    obligations TEXT,
    status VARCHAR(50) DEFAULT 'active',
    last_payment_date TIMESTAMP,
    next_payment_date TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INTEGER,
    updated_by INTEGER
);

-- Sponsorship payments table
CREATE TABLE sponsorship_payments (
    id SERIAL PRIMARY KEY,
    sponsorship_id INTEGER NOT NULL REFERENCES sponsorships(id) ON DELETE CASCADE,
    amount DECIMAL(12,2) NOT NULL,
    currency VARCHAR(3) DEFAULT 'CZK',
    payment_date TIMESTAMP NOT NULL,
    payment_method VARCHAR(100),
    reference_number VARCHAR(255),
    status VARCHAR(50) DEFAULT 'received',
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INTEGER,
    updated_by INTEGER
);

-- Sponsorship documents table
CREATE TABLE sponsorship_documents (
    id SERIAL PRIMARY KEY,
    sponsorship_id INTEGER NOT NULL REFERENCES sponsorships(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    type VARCHAR(100),
    file_name VARCHAR(500),
    file_path VARCHAR(500),
    file_size BIGINT,
    mime_type VARCHAR(100),
    description TEXT,
    version VARCHAR(20) DEFAULT '1.0',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INTEGER,
    updated_by INTEGER
);

-- Expenses table
CREATE TABLE expenses (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    category VARCHAR(100) NOT NULL,
    subcategory VARCHAR(100),
    amount DECIMAL(12,2) NOT NULL,
    currency VARCHAR(3) DEFAULT 'CZK',
    vat_rate DECIMAL(5,2) DEFAULT 21.00,
    vat_amount DECIMAL(12,2),
    total_amount DECIMAL(12,2),
    expense_date TIMESTAMP NOT NULL,
    payment_method VARCHAR(100),
    has_receipt BOOLEAN DEFAULT false,
    receipt_data TEXT,
    receipt_image VARCHAR(500),
    status VARCHAR(50) DEFAULT 'pending',
    approved_by INTEGER,
    approved_at TIMESTAMP,
    rejection_reason TEXT,
    budget_id INTEGER REFERENCES budgets(id),
    team_id INTEGER,
    project_id INTEGER,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INTEGER NOT NULL,
    updated_by INTEGER
);

-- Expense documents table
CREATE TABLE expense_documents (
    id SERIAL PRIMARY KEY,
    expense_id INTEGER NOT NULL REFERENCES expenses(id) ON DELETE CASCADE,
    name VARCHAR(255) NOT NULL,
    type VARCHAR(100),
    file_name VARCHAR(500),
    file_path VARCHAR(500),
    file_size BIGINT,
    mime_type VARCHAR(100),
    ocr_data TEXT,
    ocr_accuracy DECIMAL(5,2),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INTEGER,
    updated_by INTEGER
);

-- Indexes for performance
CREATE INDEX idx_budgets_category ON budgets(category);
CREATE INDEX idx_budgets_fiscal_year ON budgets(fiscal_year);
CREATE INDEX idx_budgets_active ON budgets(active);
CREATE INDEX idx_expenses_category ON expenses(category);
CREATE INDEX idx_expenses_status ON expenses(status);
CREATE INDEX idx_expenses_expense_date ON expenses(expense_date);
CREATE INDEX idx_expenses_budget_id ON expenses(budget_id);
CREATE INDEX idx_expenses_created_by ON expenses(created_by);
CREATE INDEX idx_sponsorships_status ON sponsorships(status);
CREATE INDEX idx_sponsorships_contract_number ON sponsorships(contract_number);
CREATE INDEX idx_sponsorship_payments_sponsorship_id ON sponsorship_payments(sponsorship_id);
CREATE INDEX idx_sponsorship_documents_sponsorship_id ON sponsorship_documents(sponsorship_id);
CREATE INDEX idx_expense_documents_expense_id ON expense_documents(expense_id);
