-- Fix portfolioprojects table structure
-- Add proper serial ID if not exists

-- First, check if the id column is serial, if not, convert it
DO $$ 
BEGIN
  -- Drop existing id column and recreate as serial
  IF EXISTS (
    SELECT 1 FROM information_schema.columns 
    WHERE table_name = 'portfolioprojects' AND column_name = 'id'
  ) THEN
    -- Create a temporary sequence if it doesn't exist
    CREATE SEQUENCE IF NOT EXISTS portfolioprojects_id_seq;
    
    -- Set the id column to use the sequence
    ALTER TABLE portfolioprojects 
    ALTER COLUMN id SET DEFAULT nextval('portfolioprojects_id_seq');
    
    -- Set the sequence to the max id + 1
    PERFORM setval('portfolioprojects_id_seq', COALESCE((SELECT MAX(id) FROM portfolioprojects), 0) + 1, false);
  END IF;
END $$;

-- Ensure we have the imageurl column
ALTER TABLE portfolioprojects ADD COLUMN IF NOT EXISTS imageurl TEXT;

-- Ensure we have proper timestamps
ALTER TABLE portfolioprojects ADD COLUMN IF NOT EXISTS createdat TIMESTAMP DEFAULT NOW();
ALTER TABLE portfolioprojects ADD COLUMN IF NOT EXISTS updatedat TIMESTAMP DEFAULT NOW();

-- Add test portfolio projects
INSERT INTO portfolioprojects (title, description, category, imageurl, isactive, createdat, updatedat)
VALUES 
(
  'Sunset Landscape Series',
  '<h2>A Beautiful Collection of Sunset Landscapes</h2>
<p>This series captures the breathtaking beauty of sunsets across different landscapes. Each piece showcases unique color palettes ranging from warm oranges and reds to cool purples and blues.</p>
<h3>Key Features:</h3>
<ul>
  <li>High-resolution digital paintings</li>
  <li>Vibrant color gradients</li>
  <li>Emotional depth and atmosphere</li>
  <li>Available in multiple sizes</li>
</ul>
<p><strong>Medium:</strong> Digital Art<br>
<strong>Year:</strong> 2024<br>
<strong>Collection:</strong> Nature Series</p>',
  'Digital Art',
  '/uploads/images/8ba675b9-c4e6-41e6-8f14-382b9ee1d019.jpg',
  true,
  NOW(),
  NOW()
),
(
  'Abstract Geometric Patterns',
  '<h2>Modern Abstract Compositions</h2>
<p>A collection of abstract artworks featuring <strong>bold geometric patterns</strong> and contemporary design elements. These pieces explore the relationship between shape, color, and space.</p>
<h3>Artistic Approach:</h3>
<ol>
  <li>Started with basic geometric shapes</li>
  <li>Layered multiple patterns and textures</li>
  <li>Applied vibrant color combinations</li>
  <li>Refined composition for visual balance</li>
</ol>
<p><em>These works are inspired by modernist movements and contemporary design trends.</em></p>',
  'Abstract',
  '/uploads/images/8ba675b9-c4e6-41e6-8f14-382b9ee1d019.jpg',
  true,
  NOW(),
  NOW()
),
(
  'Portrait Photography Collection',
  '<h2>Capturing Human Emotion</h2>
<p>This portrait series explores the <strong>depth of human emotion</strong> through carefully composed photographs. Each subject tells a unique story through their expression and body language.</p>
<h3>Technical Details:</h3>
<ul>
  <li><strong>Camera:</strong> Canon EOS R5</li>
  <li><strong>Lens:</strong> 85mm f/1.4</li>
  <li><strong>Lighting:</strong> Natural and studio</li>
  <li><strong>Processing:</strong> Adobe Lightroom & Photoshop</li>
</ul>
<p>Shot in various locations including urban settings, nature, and professional studios.</p>',
  'Photography',
  '/uploads/images/8ba675b9-c4e6-41e6-8f14-382b9ee1d019.jpg',
  true,
  NOW(),
  NOW()
),
(
  'Watercolor Botanical Illustrations',
  '<h2>Delicate Flora Studies</h2>
<p>A series of <em>hand-painted watercolor illustrations</em> featuring various botanical subjects. These pieces celebrate the intricate beauty of plants and flowers.</p>
<h3>Collection Includes:</h3>
<ul>
  <li>Wildflowers and garden blooms</li>
  <li>Tropical plants and leaves</li>
  <li>Herbs and medicinal plants</li>
  <li>Seasonal botanical studies</li>
</ul>
<blockquote>
  <p>"Nature always wears the colors of the spirit." - Ralph Waldo Emerson</p>
</blockquote>
<p>Each illustration is created using professional-grade watercolors on cold-press paper.</p>',
  'Illustration',
  '/uploads/images/8ba675b9-c4e6-41e6-8f14-382b9ee1d019.jpg',
  true,
  NOW(),
  NOW()
),
(
  'Urban Architecture Study',
  '<h2>Modern Cityscapes and Structures</h2>
<p>An exploration of <strong>contemporary urban architecture</strong> through the lens of artistic photography and digital manipulation.</p>
<h3>Focus Areas:</h3>
<ul>
  <li>Geometric building facades</li>
  <li>Glass and steel structures</li>
  <li>Reflections and symmetry</li>
  <li>Night photography and lighting</li>
</ul>
<p>This project was completed over 6 months, documenting various cities and their unique architectural personalities.</p>
<p><strong>Featured Cities:</strong> New York, Tokyo, Dubai, London</p>',
  'Photography',
  '/uploads/images/8ba675b9-c4e6-41e6-8f14-382b9ee1d019.jpg',
  false,
  NOW(),
  NOW()
)
ON CONFLICT DO NOTHING;

-- Verify the data
SELECT COUNT(*) as portfolio_count FROM portfolioprojects;
