-- Add missing price and area attributes for listing 186c318f-7b10-43d2-8eba-e1a2f88b9f39

-- First, let's check if the price and area attributes exist in the system
-- and get their IDs
WITH attribute_ids AS (
  SELECT 
    la_price.id as price_attr_id,
    la_area.id as area_attr_id
  FROM 
    (SELECT id FROM listing_attributes WHERE slug = 'price' LIMIT 1) la_price,
    (SELECT id FROM listing_attributes WHERE slug = 'area' LIMIT 1) la_area
)
-- Insert the missing attribute values
INSERT INTO listing_attribute_values (
  id, 
  listing_id, 
  attribute_id, 
  value_number, 
  created_at, 
  updated_at
)
SELECT 
  gen_random_uuid(),
  '186c318f-7b10-43d2-8eba-e1a2f88b9f39',
  attr_ids.price_attr_id,
  555555.00,  -- Default price value
  NOW(),
  NOW()
FROM attribute_ids attr_ids
WHERE NOT EXISTS (
  SELECT 1 FROM listing_attribute_values lav 
  WHERE lav.listing_id = '186c318f-7b10-43d2-8eba-e1a2f88b9f39' 
  AND lav.attribute_id = attr_ids.price_attr_id
)

UNION ALL

SELECT 
  gen_random_uuid(),
  '186c318f-7b10-43d2-8eba-e1a2f88b9f39',
  attr_ids.area_attr_id,
  555.00,  -- Default area value
  NOW(),
  NOW()
FROM attribute_ids attr_ids
WHERE NOT EXISTS (
  SELECT 1 FROM listing_attribute_values lav 
  WHERE lav.listing_id = '186c318f-7b10-43d2-8eba-e1a2f88b9f39' 
  AND lav.attribute_id = attr_ids.area_attr_id
);

-- Verify the insertion
SELECT 
  l.title,
  l.id as listing_id,
  la.slug as attribute_slug,
  lav.value_number,
  lav.created_at
FROM listings l
JOIN listing_attribute_values lav ON l.id = lav.listing_id
JOIN listing_attributes la ON lav.attribute_id = la.id
WHERE l.id = '186c318f-7b10-43d2-8eba-e1a2f88b9f39'
ORDER BY la.slug; 