-- Rename depreciation method OTHER → CUSTOM; add salvage_percentage
-- Run on each environment after deploy.

ALTER TABLE assets
  ADD COLUMN IF NOT EXISTS salvage_percentage DECIMAL(8, 4) DEFAULT 0;

COMMENT ON COLUMN assets.salvage_percentage IS 'Salvage as % of purchase_cost (0–100). Synced with salvage_value.';
COMMENT ON COLUMN assets.depreciation_method IS 'SLM, WDV, CUSTOM';

UPDATE assets
SET depreciation_method = 'CUSTOM'
WHERE depreciation_method = 'OTHER'
  AND deleted_at IS NULL;

UPDATE assets
SET salvage_percentage = ROUND(
  (COALESCE(salvage_value, 0) / purchase_cost) * 100,
  4
)
WHERE purchase_cost > 0
  AND (salvage_percentage IS NULL OR salvage_percentage = 0)
  AND COALESCE(salvage_value, 0) > 0
  AND deleted_at IS NULL;

UPDATE item_categories
SET default_depreciation_method = 'CUSTOM'
WHERE default_depreciation_method = 'OTHER';
