-- Create financial management tables
-- Migration: 20250110000001_create_financial_tables

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

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);

-- 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),
    currency VARCHAR(3) DEFAULT 'CZK',
    start_date TIMESTAMP,
    end_date TIMESTAMP,
    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
);

CREATE INDEX idx_sponsorships_status ON sponsorships(status);
CREATE INDEX idx_sponsorships_contract_number ON sponsorships(contract_number);
CREATE INDEX idx_sponsorships_end_date ON sponsorships(end_date);

-- 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),
    currency VARCHAR(3) DEFAULT 'CZK',
    payment_date TIMESTAMP,
    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
);

CREATE INDEX idx_sponsorship_payments_sponsorship_id ON sponsorship_payments(sponsorship_id);
CREATE INDEX idx_sponsorship_payments_payment_date ON sponsorship_payments(payment_date);
CREATE INDEX idx_sponsorship_payments_status ON sponsorship_payments(status);

-- 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
);

CREATE INDEX idx_sponsorship_documents_sponsorship_id ON sponsorship_documents(sponsorship_id);
CREATE INDEX idx_sponsorship_documents_type ON sponsorship_documents(type);

-- Expenses table
CREATE TABLE expenses (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    category VARCHAR(100),
    subcategory VARCHAR(100),
    amount DECIMAL(12,2),
    currency VARCHAR(3) DEFAULT 'CZK',
    vat_rate DECIMAL(5,2) DEFAULT 21,
    vat_amount DECIMAL(12,2),
    total_amount DECIMAL(12,2),
    expense_date TIMESTAMP,
    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,
    updated_by INTEGER
);

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_team_id ON expenses(team_id);
CREATE INDEX idx_expenses_created_by ON expenses(created_by);

-- 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
);

CREATE INDEX idx_expense_documents_expense_id ON expense_documents(expense_id);
CREATE INDEX idx_expense_documents_type ON expense_documents(type);

-- Financial reports table
CREATE TABLE financial_reports (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    type VARCHAR(100),
    period VARCHAR(50),
    report_data TEXT,
    summary TEXT,
    file_path VARCHAR(500),
    generated_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by INTEGER,
    updated_by INTEGER
);

CREATE INDEX idx_financial_reports_type ON financial_reports(type);
CREATE INDEX idx_financial_reports_period ON financial_reports(period);
CREATE INDEX idx_financial_reports_generated_at ON financial_reports(generated_at);

-- Financial settings table
CREATE TABLE financial_settings (
    id SERIAL PRIMARY KEY,
    default_currency VARCHAR(3) DEFAULT 'CZK',
    default_vat_rate DECIMAL(5,2) DEFAULT 21,
    fiscal_year_start VARCHAR(10) DEFAULT '01-01',
    expense_approval_required BOOLEAN DEFAULT true,
    max_expense_auto_approve DECIMAL(12,2) DEFAULT 1000,
    budget_alert_enabled BOOLEAN DEFAULT true,
    budget_alert_threshold DECIMAL(5,2) DEFAULT 80,
    sponsorship_alert_enabled BOOLEAN DEFAULT true,
    ocr_service_enabled BOOLEAN DEFAULT true,
    ocr_provider VARCHAR(50) DEFAULT 'tesseract',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by INTEGER
);

-- Insert default financial settings
INSERT INTO financial_settings (
    default_currency,
    default_vat_rate,
    fiscal_year_start,
    expense_approval_required,
    max_expense_auto_approve,
    budget_alert_enabled,
    budget_alert_threshold,
    sponsorship_alert_enabled,
    ocr_service_enabled,
    ocr_provider
) VALUES (
    'CZK',
    21.0,
    '01-01',
    true,
    1000.00,
    true,
    80.0,
    true,
    true,
    'tesseract'
);

-- Create default budget categories for a typical football club
INSERT INTO budgets (
    name,
    description,
    category,
    yearly_limit,
    monthly_limit,
    fiscal_year,
    start_date,
    end_date,
    active
) VALUES 
('Týmové provoz', 'Mzdy hráčů, trenérů a realizačního týmu', 'Týmové provoz', 500000.00, 41667.00, 2025, '2025-01-01 00:00:00', '2025-12-31 23:59:59', true),
('Stadion a zařízení', 'Údržba stadionu, energie, voda, odpady', 'Stadion', 200000.00, 16667.00, 2025, '2025-01-01 00:00:00', '2025-12-31 23:59:59', true),
('Cestování', 'Ubytování, doprava na zápasy a soustředění', 'Cestování', 150000.00, 12500.00, 2025, '2025-01-01 00:00:00', '2025-12-31 23:59:59', true),
('Materiál a vybavení', 'Sportovní vybavení, dresy, míče', 'Materiál', 100000.00, 8333.00, 2025, '2025-01-01 00:00:00', '2025-12-31 23:59:59', true),
('Marketing a reklama', 'Propagace, reklama, sociální sítě', 'Marketing', 80000.00, 6667.00, 2025, '2025-01-01 00:00:00', '2025-12-31 23:59:59', true),
('Administrativa', 'Kancelářské potřeby, software, právní služby', 'Administrativa', 50000.00, 4167.00, 2025, '2025-01-01 00:00:00', '2025-12-31 23:59:59', true);

-- Create trigger to update updated_at column
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

-- Apply the trigger to all financial tables
CREATE TRIGGER update_budgets_updated_at BEFORE UPDATE ON budgets FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_sponsorships_updated_at BEFORE UPDATE ON sponsorships FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_sponsorship_payments_updated_at BEFORE UPDATE ON sponsorship_payments FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_sponsorship_documents_updated_at BEFORE UPDATE ON sponsorship_documents FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_expenses_updated_at BEFORE UPDATE ON expenses FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_expense_documents_updated_at BEFORE UPDATE ON expense_documents FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_financial_reports_updated_at BEFORE UPDATE ON financial_reports FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_financial_settings_updated_at BEFORE UPDATE ON financial_settings FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
