/** Flarlo Urheberrecht – Client-Prüfung (spiegelt copyright.js). */
(function (global) {
  const DEFAULT_POLICY = {
    music: { label: 'Musik', maxExcerptSec: 30 },
    film: { label: 'Filme & Serien', maxExcerptSec: 60 },
    musicNicheIds: [
      'dance', 'musik', 'singing', 'kpop', 'hiphop', 'deutschrap', 'edm', 'rock', 'pop',
      'schlager', 'latin', 'klassik', 'instruments', 'dj',
    ],
    filmNicheIds: ['film', 'anime', 'cosplay', 'sketches', 'reactions', 'horror', 'truecrime'],
  };

  const MUSIC_KEYWORDS = [
    'song', 'musik', 'music', 'track', 'album', 'remix', 'cover', 'soundtrack', 'lied', 'single',
    'spotify', 'beat', 'karaoke', 'playlist', 'rapper', 'charts', 'audio',
  ];
  const FILM_KEYWORDS = [
    'film', 'movie', 'serie', 'serien', 'netflix', 'disney', 'marvel', 'trailer', 'szene', 'scene',
    'kinofilm', 'blockbuster', 'folge', 'episode', 'staffel', 'season', 'anime', 'manga',
    'hbo', 'primevideo', 'crunchyroll',
  ];
  const FULL_CONTENT_PHRASES = [
    'ganzer film', 'ganze folge', 'kompletter film', 'komplette folge', 'komplette serie',
    'full movie', 'full film', 'full episode', 'entire movie', 'whole movie',
    'ganzes lied', 'ganzer song', 'kompletter song', 'komplettes album', 'ganzes album',
    'full song', 'full album', 'ungeschnitten', 'directors cut', 'komplett stream',
  ];

  let policy = DEFAULT_POLICY;

  function setPolicy(p) {
    policy = Object.assign({}, DEFAULT_POLICY, p || {});
    policy.music = Object.assign({}, DEFAULT_POLICY.music, (p && p.music) || {});
    policy.film = Object.assign({}, DEFAULT_POLICY.film, (p && p.film) || {});
  }

  function haystack(caption, hashtags, niches) {
    const tags = (hashtags || []).join(' ');
    const nicheStr = (niches || []).join(' ');
    return (caption || '') + ' ' + tags + ' ' + nicheStr;
  }

  function detectCategory(caption, hashtags, niches) {
    const text = haystack(caption, hashtags, niches).toLowerCase();
    const musicNiches = new Set(policy.musicNicheIds || DEFAULT_POLICY.musicNicheIds);
    const filmNiches = new Set(policy.filmNicheIds || DEFAULT_POLICY.filmNicheIds);
    let music = false;
    let film = false;
    (niches || []).forEach((id) => {
      if (musicNiches.has(id)) music = true;
      if (filmNiches.has(id)) film = true;
    });
    (hashtags || []).forEach((tag) => {
      const t = String(tag).toLowerCase();
      if (musicNiches.has(t)) music = true;
      if (filmNiches.has(t)) film = true;
    });
    MUSIC_KEYWORDS.forEach((w) => {
      if (text.includes(w)) music = true;
    });
    FILM_KEYWORDS.forEach((w) => {
      if (text.includes(w)) film = true;
    });
    if (music) return 'music';
    if (film) return 'film';
    return null;
  }

  function check(opts) {
    opts = opts || {};
    const caption = opts.caption || '';
    const hashtags = opts.hashtags || [];
    const niches = opts.niches || [];
    const durationSec = opts.durationSec || 0;
    const type = opts.type || 'video';
    const text = haystack(caption, hashtags, niches).toLowerCase();

    for (let i = 0; i < FULL_CONTENT_PHRASES.length; i++) {
      if (text.includes(FULL_CONTENT_PHRASES[i])) {
        return {
          ok: false,
          reason: 'Keine kompletten Filme, Folgen oder Songs – nur Teilausschnitte erlaubt.',
        };
      }
    }

    if (type !== 'video' || !durationSec) return { ok: true, category: null };

    const category = detectCategory(caption, hashtags, niches);
    if (!category) return { ok: true, category: null };

    const maxSec =
      category === 'music' ? policy.music.maxExcerptSec : policy.film.maxExcerptSec;
    if (durationSec > maxSec + 0.5) {
      const label = category === 'music' ? policy.music.label : policy.film.label;
      return {
        ok: false,
        reason:
          label + ': nur Teilausschnitte bis ' + maxSec + 's (dein Clip: ' + Math.ceil(durationSec) + 's).',
        category,
        maxSec,
      };
    }
    return { ok: true, category, maxSec };
  }

  function hintFor(category) {
    if (!category) return '';
    if (category === 'music') {
      return 'Musik erkannt: max. ' + policy.music.maxExcerptSec + 's Teilausschnitt.';
    }
    return 'Film/Serie erkannt: max. ' + policy.film.maxExcerptSec + 's Teilausschnitt.';
  }

  global.VlCopyright = { setPolicy, check, detectCategory, hintFor };
})(typeof window !== 'undefined' ? window : globalThis);
