-- Migration: Add TCO (Total Cost of Ownership) fields to vehicles table
-- Issue: #15

ALTER TABLE vehicles
  ADD COLUMN IF NOT EXISTS purchase_price DECIMAL(12,2),
  ADD COLUMN IF NOT EXISTS purchase_date DATE,
  ADD COLUMN IF NOT EXISTS insurance_cost DECIMAL(10,2),
  ADD COLUMN IF NOT EXISTS insurance_interval VARCHAR(20),
  ADD COLUMN IF NOT EXISTS registration_cost DECIMAL(10,2),
  ADD COLUMN IF NOT EXISTS registration_interval VARCHAR(20),
  ADD COLUMN IF NOT EXISTS tco_enabled BOOLEAN DEFAULT false;

-- Add CHECK constraints to enforce valid interval values
ALTER TABLE vehicles
  ADD CONSTRAINT chk_insurance_interval
    CHECK (insurance_interval IS NULL OR insurance_interval IN ('monthly', 'semi_annual', 'annual'));

ALTER TABLE vehicles
  ADD CONSTRAINT chk_registration_interval
    CHECK (registration_interval IS NULL OR registration_interval IN ('monthly', 'semi_annual', 'annual'));

-- Add CHECK constraints for non-negative costs
ALTER TABLE vehicles
  ADD CONSTRAINT chk_purchase_price_non_negative
    CHECK (purchase_price IS NULL OR purchase_price >= 0);

ALTER TABLE vehicles
  ADD CONSTRAINT chk_insurance_cost_non_negative
    CHECK (insurance_cost IS NULL OR insurance_cost >= 0);

ALTER TABLE vehicles
  ADD CONSTRAINT chk_registration_cost_non_negative
    CHECK (registration_cost IS NULL OR registration_cost >= 0);
