-- Sample seed data for local development / DataGrip testing
-- Password for all users: password
--
-- User types:
--   1 Ali     — super_admin
--   2 Reza    — business_owner (Meshkee Demo Shop)
--   3 Sara    — business_owner (Creative Studio)
--   4 Mina    — customer on shop-a.local
--   5 Amir    — customer on studio-b.local

INSERT INTO users (id, cell_number, password_hash, email, first_name, last_name, cell_verified_at) VALUES
  (1, '+989121111111', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'ali@meshkee.demo',   'Ali',  'Hassani', NOW()),
  (2, '+989122222222', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'reza@shop.demo',     'Reza', 'Ahmadi',  NOW()),
  (3, '+989123333333', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'sara@studio.demo',   'Sara', 'Karimi',  NOW()),
  (4, '+989124444444', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'mina@customer.demo', 'Mina', 'Salehi',  NOW()),
  (5, '+989125555555', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'amir@customer.demo', 'Amir', 'Jafari',  NOW())
ON CONFLICT (id) DO NOTHING;

INSERT INTO businesses (id, name, name_fa, about, slug, description) VALUES
  (1, 'Meshkee Demo Shop', 'فروشگاه دمو مشکی', 'فروشگاه آنلاین نمونه برای تست سیستم', 'meshkee-demo-shop', 'Sample e-commerce business'),
  (2, 'Creative Studio',   'استودیو خلاق',     'آژانس طراحی و برندینگ', 'creative-studio',   'Design and branding agency')
ON CONFLICT (id) DO NOTHING;

-- System business categories are seeded in 005_business_categories.sql

INSERT INTO domains (id, business_id, host, is_primary, is_verified, verified_at, ssl_enabled) VALUES
  (1, 1, 'shop-a.local',     TRUE,  TRUE, NOW(), FALSE),
  (2, 1, 'www.shop-a.local', FALSE, TRUE, NOW(), FALSE),
  (3, 2, 'studio-b.local',   TRUE,  TRUE, NOW(), FALSE)
ON CONFLICT (id) DO NOTHING;

-- Roles
INSERT INTO user_roles (user_id, role_id)
SELECT 1, r.id FROM roles r WHERE r.slug = 'super_admin'
ON CONFLICT (user_id, role_id) DO NOTHING;

INSERT INTO user_roles (user_id, role_id)
SELECT 2, r.id FROM roles r WHERE r.slug = 'business_owner'
ON CONFLICT (user_id, role_id) DO NOTHING;

INSERT INTO user_roles (user_id, role_id)
SELECT 3, r.id FROM roles r WHERE r.slug = 'business_owner'
ON CONFLICT (user_id, role_id) DO NOTHING;

INSERT INTO user_roles (user_id, role_id)
SELECT 4, r.id FROM roles r WHERE r.slug = 'customer'
ON CONFLICT (user_id, role_id) DO NOTHING;

INSERT INTO user_roles (user_id, role_id)
SELECT 5, r.id FROM roles r WHERE r.slug = 'customer'
ON CONFLICT (user_id, role_id) DO NOTHING;

-- Business owners (assigned by super admin)
INSERT INTO business_users (id, business_id, user_id, is_owner) VALUES
  (1, 1, 2, TRUE),
  (2, 2, 3, TRUE)
ON CONFLICT (id) DO NOTHING;

-- Team staff: editor on business 1 (invited by business owner)
INSERT INTO users (id, cell_number, password_hash, email, first_name, last_name, cell_verified_at) VALUES
  (6, '+989126666667', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'editor@shop.demo', 'Nima', 'Editori', NOW())
ON CONFLICT (id) DO NOTHING;

INSERT INTO business_users (id, business_id, user_id, is_owner, role_id, invited_by)
SELECT 3, 1, 6, FALSE, r.id, 2
FROM roles r WHERE r.slug = 'editor'
ON CONFLICT (id) DO NOTHING;

INSERT INTO user_roles (user_id, role_id)
SELECT 6, r.id FROM roles r WHERE r.slug = 'business_staff'
ON CONFLICT (user_id, role_id) DO NOTHING;

-- Website customers
INSERT INTO business_customers (id, business_id, user_id) VALUES
  (1, 1, 4),
  (2, 2, 5)
ON CONFLICT (id) DO NOTHING;

INSERT INTO media (
  id, business_id, uploaded_by, media_type, storage_disk, storage_path, public_url,
  file_name, original_file_name, mime_type, file_size_bytes, width, height, duration_seconds, alt_text
) VALUES
  (1, 1, 2, 'image', 'local', '/uploads/shop/hero-phone.jpg',  'https://cdn.meshkee.demo/shop/hero-phone.jpg',  'hero-phone.jpg',  'hero-phone.jpg',  'image/jpeg', 245000, 1200, 800, NULL, 'Smartphone hero'),
  (2, 1, 2, 'image', 'local', '/uploads/shop/laptop.jpg',      'https://cdn.meshkee.demo/shop/laptop.jpg',      'laptop.jpg',      'laptop.jpg',      'image/jpeg', 198000, 1200, 800, NULL, 'Laptop product shot'),
  (3, 1, 2, 'video', 'local', '/uploads/shop/unboxing.mp4',    'https://cdn.meshkee.demo/shop/unboxing.mp4',    'unboxing.mp4',    'unboxing.mp4',    'video/mp4',  5200000, NULL, NULL, 42.50, 'Product unboxing'),
  (4, 2, 3, 'image', 'local', '/uploads/studio/brand-cover.jpg', 'https://cdn.meshkee.demo/studio/brand-cover.jpg', 'brand-cover.jpg', 'brand-cover.jpg', 'image/jpeg', 310000, 1600, 900, NULL, 'Branding project cover'),
  (5, 2, 3, 'image', 'local', '/uploads/studio/web-ui.jpg',    'https://cdn.meshkee.demo/studio/web-ui.jpg',    'web-ui.jpg',      'web-ui.jpg',      'image/jpeg', 275000, 1440, 900, NULL, 'Web design mockup')
ON CONFLICT (id) DO NOTHING;

INSERT INTO categories (id, business_id, entity_type, parent_id, name, slug, description, sort_order) VALUES
  (1,  1, 'product',   NULL, 'Electronics',         'electronics',         'Electronic devices', 1),
  (2,  1, 'product',   1,    'Phones',              'phones',              'Mobile phones', 1),
  (3,  1, 'product',   1,    'Laptops',             'laptops',             'Laptops and notebooks', 2),
  (4,  1, 'product',   NULL, 'Accessories',         'accessories',         'Phone and laptop accessories', 2),
  (5,  1, 'blog',      NULL, 'Tutorials',           'tutorials',           'How-to guides', 1),
  (6,  1, 'blog',      NULL, 'News',                'news',                'Store news and updates', 2),
  (7,  1, 'portfolio', NULL, 'Product Photography', 'product-photography', 'Commercial product shoots', 1),
  (8,  2, 'product',   NULL, 'Design Packages', 'design-packages', 'Service packages', 1),
  (9,  2, 'blog',      NULL, 'Case Studies',    'case-studies',    'Client success stories', 1),
  (10, 2, 'blog',      NULL, 'Design Tips',     'design-tips',     'Tips for better design', 2),
  (11, 2, 'portfolio', NULL, 'Branding',        'branding',        'Logo and identity work', 1),
  (12, 2, 'portfolio', NULL, 'Web Design',      'web-design',      'Websites and web apps', 2)
ON CONFLICT (id) DO NOTHING;

INSERT INTO products (
  id, business_id, title, slug, description, content, price, compare_at_price, sku,
  stock_quantity, status, featured_media_id, sort_order, published_at
) VALUES
  (1, 1, 'Meshkee X Phone', 'meshkee-x-phone', 'Flagship smartphone with OLED display.',
   '{"blocks":[{"type":"paragraph","text":"6.5 inch OLED, 256GB storage."}]}',
   12990000, 14990000, 'MXP-001', 25, 'published', 1, 1, NOW()),
  (2, 1, 'Meshkee Book Pro', 'meshkee-book-pro', 'Lightweight laptop for creators.',
   '{"blocks":[{"type":"paragraph","text":"14 inch, 16GB RAM, 512GB SSD."}]}',
   28990000, NULL, 'MBP-001', 10, 'published', 2, 2, NOW()),
  (3, 2, 'Brand Identity Package', 'brand-identity-package', 'Logo, color palette, and brand guidelines.',
   '{"blocks":[{"type":"paragraph","text":"Includes 3 logo concepts."}]}',
   15000000, NULL, 'PKG-BRAND-01', NULL, 'published', 4, 1, NOW())
ON CONFLICT (id) DO NOTHING;

INSERT INTO blogs (
  id, business_id, author_id, title, slug, excerpt, content, status, featured_media_id, published_at
) VALUES
  (1, 1, 2, 'How to Choose the Right Phone', 'how-to-choose-phone',
   'A quick guide to picking your next smartphone.',
   '{"blocks":[{"type":"heading","text":"Battery life"},{"type":"paragraph","text":"Look for 4000mAh or more."}]}',
   'published', 1, NOW()),
  (2, 1, 2, 'Summer Sale Starts Next Week', 'summer-sale-next-week',
   'Up to 30% off on selected electronics.',
   '{"blocks":[{"type":"paragraph","text":"Sale runs Monday through Sunday."}]}',
   'draft', NULL, NULL),
  (3, 2, 3, 'Rebranding a Local Café', 'rebranding-local-cafe',
   'How we refreshed a neighborhood café brand.',
   '{"blocks":[{"type":"paragraph","text":"We started with customer interviews."}]}',
   'published', 4, NOW())
ON CONFLICT (id) DO NOTHING;

INSERT INTO portfolios (
  id, business_id, title, slug, description, content, client_name, project_url,
  status, featured_media_id, sort_order, published_at
) VALUES
  (1, 1, 'Phone Launch Campaign', 'phone-launch-campaign',
   'Product photos and video for Meshkee X Phone launch.',
   '{"blocks":[{"type":"paragraph","text":"Shot in studio with 3 lighting setups."}]}',
   'Meshkee', 'https://shop-a.local/products/meshkee-x-phone', 'published', 1, 1, NOW()),
  (2, 2, 'Nova Café Rebrand', 'nova-cafe-rebrand',
   'Full brand identity for Nova Café.',
   '{"blocks":[{"type":"paragraph","text":"Logo, menu design, and signage."}]}',
   'Nova Café', 'https://novacafe.example.com', 'published', 4, 1, NOW()),
  (3, 2, 'FinTech Dashboard UI', 'fintech-dashboard-ui',
   'Dashboard design for a financial startup.',
   '{"blocks":[{"type":"paragraph","text":"Dark mode first design system."}]}',
   'PayFlow', 'https://payflow.example.com', 'published', 5, 2, NOW())
ON CONFLICT (id) DO NOTHING;

INSERT INTO category_assignments (id, business_id, category_id, entity_type, entity_id) VALUES
  (1, 1, 2,  'product',   1),
  (2, 1, 3,  'product',   2),
  (3, 2, 8,  'product',   3),
  (4, 1, 5,  'blog',      1),
  (5, 1, 6,  'blog',      2),
  (6, 2, 9,  'blog',      3),
  (7, 1, 7,  'portfolio', 1),
  (8, 2, 11, 'portfolio', 2),
  (9, 2, 12, 'portfolio', 3)
ON CONFLICT (id) DO NOTHING;

INSERT INTO media_attachments (id, business_id, media_id, entity_type, entity_id, sort_order, is_featured) VALUES
  (1, 1, 3, 'product',   1, 1, FALSE),
  (2, 2, 5, 'portfolio', 3, 1, FALSE)
ON CONFLICT (id) DO NOTHING;

SELECT setval(pg_get_serial_sequence('users', 'id'),               COALESCE((SELECT MAX(id) FROM users), 1));
SELECT setval(pg_get_serial_sequence('businesses', 'id'),          COALESCE((SELECT MAX(id) FROM businesses), 1));
SELECT setval(pg_get_serial_sequence('domains', 'id'),             COALESCE((SELECT MAX(id) FROM domains), 1));
SELECT setval(pg_get_serial_sequence('business_users', 'id'),     COALESCE((SELECT MAX(id) FROM business_users), 1));
SELECT setval(pg_get_serial_sequence('business_customers', 'id'),   COALESCE((SELECT MAX(id) FROM business_customers), 1));
SELECT setval(pg_get_serial_sequence('media', 'id'),               COALESCE((SELECT MAX(id) FROM media), 1));
SELECT setval(pg_get_serial_sequence('categories', 'id'),         COALESCE((SELECT MAX(id) FROM categories), 1));
SELECT setval(pg_get_serial_sequence('products', 'id'),           COALESCE((SELECT MAX(id) FROM products), 1));
SELECT setval(pg_get_serial_sequence('blogs', 'id'),              COALESCE((SELECT MAX(id) FROM blogs), 1));
SELECT setval(pg_get_serial_sequence('portfolios', 'id'),         COALESCE((SELECT MAX(id) FROM portfolios), 1));
SELECT setval(pg_get_serial_sequence('category_assignments', 'id'), COALESCE((SELECT MAX(id) FROM category_assignments), 1));
SELECT setval(pg_get_serial_sequence('media_attachments', 'id'), COALESCE((SELECT MAX(id) FROM media_attachments), 1));
