from ytplom.db import Hash, DbConn
 from ytplom.misc import (
         FilesWithIndex, FlagName, PlayerUpdateId, QueryId, QueryText,
-        QuotaCost, UrlStr, YoutubeId,
+        QuotaCost, Tag, UrlStr, YoutubeId,
         FILE_FLAGS, PATH_THUMBNAILS, YOUTUBE_URL_PREFIX,
         ensure_expected_dirs,
         Config, DownloadsManager, Player, QuotaLog, VideoFile, YoutubeQuery,
         None | bool
         | FilesWithIndex | _PageNames | _ParamsStr | Path | PlayerUpdateId
         | QueryText | QuotaCost | UrlStr | 'VideoFile' | YoutubeId
-        | 'YoutubeVideo' | list[FlagName] | list['VideoFile']
+        | 'YoutubeVideo' | list[FlagName] | list['Tag'] | list['VideoFile']
         | list['YoutubeVideo'] | list['YoutubeQuery']
 ]
 
         with DbConn() as conn:
             file = VideoFile.get_one(conn, digest)
             file.set_flags([FILE_FLAGS[name] for name in flag_names])
-            file.tags = postvars.get('tags', [])
+            file.tags = [Tag(t) for t in postvars.get('tags', [])]
             file.save(conn)
             conn.commit()
         file.ensure_absence_if_deleted()
     def _send_file_data(self, digest: Hash) -> None:
         with DbConn() as conn:
             file = VideoFile.get_one(conn, digest)
+            all_tags = VideoFile.get_all_tags(conn)
         self._send_rendered_template(
                 _NAME_TEMPLATE_FILE_DATA,
-                {'file': file, 'flag_names': list(FILE_FLAGS)})
+                {'file': file, 'flag_names': list(FILE_FLAGS),
+                 'unused_tags': [t for t in all_tags if t not in file.tags]})
 
     def _send_files_index(self,
                           filter_: _ParamsStr,
 
 ProseText = NewType('ProseText', str)
 FlagName = NewType('FlagName', str)
 FlagsInt = NewType('FlagsInt', int)
+Tag = NewType('Tag', str)
 AmountDownloads = NewType('AmountDownloads', int)
 PlayerUpdateId = NewType('PlayerUpdateId', str)
 UrlStr = NewType('UrlStr', str)
     last_update: DatetimeStr
     rel_path: Path
     digest: Hash
+    tags: list[Tag]
 
     def __init__(self,
                  digest: Optional[Hash],
         self.rel_path = rel_path
         self.digest = digest if digest else Hash.from_file(self.full_path)
         self.flags = flags
-        self.tags = tags_str.split(',') if tags_str else []
+        self.tags = [Tag(t) for t in tags_str.split(',')] if tags_str else []
         self.yt_id = yt_id
         if last_update is None:
             self._renew_last_update()
             raise NotFoundException(f'no entry for file to Youtube ID {yt_id}')
         return cls._from_table_row(row)
 
+    @classmethod
+    def get_all_tags(cls, conn: BaseDbConn) -> set[Tag]:
+        """Return all tags used among VideoFiles."""
+        tags = set()
+        for file in cls.get_all(conn):
+            for tag in file.tags:
+                tags.add(tag)
+        return tags
+
     @property
     def tags_str(self):
         """Return self.tags joined by ','."""