-- Migration: Create ownership_costs table
-- Issue: #29
-- Description: Create ownership_costs table for tracking insurance, registration, tax, inspection, parking, and other costs

CREATE TABLE ownership_costs (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  user_id VARCHAR(255) NOT NULL,
  vehicle_id UUID NOT NULL REFERENCES vehicles(id) ON DELETE CASCADE,
  document_id UUID NULL REFERENCES documents(id) ON DELETE CASCADE,
  cost_type VARCHAR(32) NOT NULL CHECK (cost_type IN ('insurance', 'registration', 'tax', 'inspection', 'parking', 'other')),
  amount DECIMAL(10, 2) NOT NULL CHECK (amount > 0),
  description VARCHAR(200),
  period_start DATE,
  period_end DATE,
  notes TEXT,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_ownership_costs_user_id ON ownership_costs(user_id);
CREATE INDEX idx_ownership_costs_vehicle_id ON ownership_costs(vehicle_id);
CREATE INDEX idx_ownership_costs_document_id ON ownership_costs(document_id);
CREATE INDEX idx_ownership_costs_cost_type ON ownership_costs(cost_type);

CREATE TRIGGER update_ownership_costs_updated_at
  BEFORE UPDATE ON ownership_costs
  FOR EACH ROW
  EXECUTE FUNCTION update_updated_at_column();
