CREATE TABLE IF NOT EXISTS video_summary AS
SELECT
    v.file_name,
    v.raw_file,
    v.mp4_file,
    v.record_datetime,
    v.day_of_week,
    v.difficulty_level,
    v.address,
    GROUP_CONCAT(DISTINCT l.name) AS labels,
    GROUP_CONCAT(DISTINCT p.name) AS playlists
FROM videos v
LEFT JOIN video_labels vl ON vl.video_file_name = v.file_name
LEFT JOIN labels l ON l.id = vl.label_id
LEFT JOIN video_playlists vp ON vp.video_file_name = v.file_name
LEFT JOIN playlists p ON p.id = vp.playlist_id
GROUP BY v.file_name;

DROP VIEW playlist_videos;
CREATE VIEW playlist_videos AS
WITH
-- Sous-requête pour les playlists manuelles
manual_playlist_videos AS (
  SELECT
    p.id AS playlist_id,
    p.name AS playlist_name,
    vp.video_file_name,
    v.*,
    'manual' AS playlist_type
  FROM playlists p
  JOIN video_playlists vp ON p.id = vp.playlist_id
  JOIN videos v ON vp.video_file_name = v.file_name
  WHERE p.type = 'manual'
),

-- Sous-requête pour les playlists dynamiques
dynamic_playlist_videos AS (
  SELECT DISTINCT
    p.id AS playlist_id,
    p.name AS playlist_name,
    v.file_name AS video_file_name,
    v.*,
    'dynamic' AS playlist_type
  FROM playlists p
  JOIN videos v
  WHERE p.type = 'dynamic'
    -- Inclure les vidéos qui ont les labels requis
    AND (
      json_array_length(json_extract(p.rules_json, '$.include_labels')) = 0
      OR EXISTS (
        SELECT 1 FROM labels l
        JOIN video_labels vl ON l.id = vl.label_id
        WHERE vl.video_file_name = v.file_name
          AND EXISTS (
            SELECT 1
            FROM (
              SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
              -- Ajoute plus de lignes si tes tableaux JSON ont plus de 5 éléments
            ) n
            WHERE n.n < json_array_length(json_extract(p.rules_json, '$.include_labels'))
              AND l.name = json_extract(
                json_extract(p.rules_json, '$.include_labels'),
                '$[' || n.n || ']'
              )
          )
      )
    )
    -- Exclure les vidéos qui ont les labels exclus
    AND NOT EXISTS (
      SELECT 1 FROM labels l
      JOIN video_labels vl ON l.id = vl.label_id
      WHERE vl.video_file_name = v.file_name
        AND EXISTS (
          SELECT 1
          FROM (
            SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
          ) n
          WHERE n.n < json_array_length(json_extract(p.rules_json, '$.exclude_labels'))
            AND l.name = json_extract(
              json_extract(p.rules_json, '$.exclude_labels'),
              '$[' || n.n || ']'
            )
        )
    )
    -- Inclure les vidéos qui sont dans les playlists requises
    AND (
      json_array_length(json_extract(p.rules_json, '$.include_playlists')) = 0
      OR EXISTS (
        SELECT 1 FROM playlists pl
        JOIN video_playlists vp ON pl.id = vp.playlist_id
        WHERE vp.video_file_name = v.file_name
          AND EXISTS (
            SELECT 1
            FROM (
              SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
            ) n
            WHERE n.n < json_array_length(json_extract(p.rules_json, '$.include_playlists'))
              AND pl.name = json_extract(
                json_extract(p.rules_json, '$.include_playlists'),
                '$[' || n.n || ']'
              )
          )
      )
    )
    -- Exclure les vidéos qui sont dans les playlists exclues
    AND NOT EXISTS (
      SELECT 1 FROM playlists pl
      JOIN video_playlists vp ON pl.id = vp.playlist_id
      WHERE vp.video_file_name = v.file_name
        AND EXISTS (
          SELECT 1
          FROM (
            SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
          ) n
          WHERE n.n < json_array_length(json_extract(p.rules_json, '$.exclude_playlists'))
            AND pl.name = json_extract(
              json_extract(p.rules_json, '$.exclude_playlists'),
              '$[' || n.n || ']'
            )
        )
    )
    -- Filtrer par date si nécessaire
    AND (
      json_extract(p.rules_json, '$.date_after') IS NULL
      OR v.record_datetime >= json_extract(p.rules_json, '$.date_after')
    )
    AND (
      json_extract(p.rules_json, '$.date_before') IS NULL
      OR v.record_datetime <= json_extract(p.rules_json, '$.date_before')
    )
)

-- Union des deux types de playlists
SELECT * FROM manual_playlist_videos
UNION ALL
SELECT * FROM dynamic_playlist_videos;
