import '../../marianumcloud/webdav/queries/list_files/cacheable_file.dart';
import '../../marianumcloud/webdav/queries/list_files/list_files_response.dart';

/// Curated WebDAV listings for the demo mode. Everything is built on
/// [CacheableFile] (the app-level model the file UI renders) so no `nextcloud`
/// DAV objects need to be faked and no network is ever touched.
///
/// `hasPreview`/`fileId` are left null on purpose: without them the file list
/// won't try to fetch server-side thumbnails for the fixtures.
class DemoFiles {
  const DemoFiles._();

  /// Returns the listing for [path] as passed to `FilesDataProvider.listFiles`
  /// (`/` or empty for the root, otherwise the joined folder display names).
  /// Unknown folders resolve to an empty listing rather than an error.
  static ListFilesResponse listing(String path) {
    final key = path.replaceAll('/', '').trim();
    switch (key) {
      case '':
        return _root();
      case 'Hausaufgaben':
        return _hausaufgaben();
      case 'Arbeitsblätter':
        return _arbeitsblaetter();
      case 'Klausuren':
        return _klausuren();
      case 'Schuljahr 2025-26':
        return _schuljahr();
      default:
        return ListFilesResponse({});
    }
  }

  static ListFilesResponse _root() => ListFilesResponse({
    _folder('Hausaufgaben', daysAgo: 1),
    _folder('Arbeitsblätter', daysAgo: 3),
    _folder('Klausuren', daysAgo: 12),
    _folder('Schuljahr 2025-26', daysAgo: 30),
    _file('Deutsch Lektüre.pdf', size: 2_480_000, daysAgo: 2),
    _file('Mathe Formelsammlung.pdf', size: 890_000, daysAgo: 9),
    _file('Bio Referat.pptx', size: 5_120_000, daysAgo: 5),
    _file('Stundenplan.pdf', size: 145_000, daysAgo: 14),
  });

  static ListFilesResponse _hausaufgaben() => ListFilesResponse({
    _file(
      'Deutsch Aufsatz.docx',
      parent: 'Hausaufgaben',
      size: 42_000,
      daysAgo: 1,
    ),
    _file(
      'Mathe Übungen KW28.pdf',
      parent: 'Hausaufgaben',
      size: 310_000,
      daysAgo: 2,
    ),
    _file(
      'Englisch Vokabeln.pdf',
      parent: 'Hausaufgaben',
      size: 96_000,
      daysAgo: 4,
    ),
  });

  static ListFilesResponse _arbeitsblaetter() => ListFilesResponse({
    _file(
      'Physik Optik.pdf',
      parent: 'Arbeitsblätter',
      size: 720_000,
      daysAgo: 6,
    ),
    _file(
      'Geschichte Weimar.pdf',
      parent: 'Arbeitsblätter',
      size: 540_000,
      daysAgo: 8,
    ),
  });

  static ListFilesResponse _klausuren() => ListFilesResponse({
    _file(
      'Mathe Klausur 1.pdf',
      parent: 'Klausuren',
      size: 630_000,
      daysAgo: 20,
    ),
    _file(
      'Deutsch Klausur 1.pdf',
      parent: 'Klausuren',
      size: 580_000,
      daysAgo: 34,
    ),
  });

  static ListFilesResponse _schuljahr() => ListFilesResponse({
    _folder('Halbjahr 1', parent: 'Schuljahr 2025-26', daysAgo: 40),
    _file(
      'Klassenliste 10b.pdf',
      parent: 'Schuljahr 2025-26',
      size: 78_000,
      daysAgo: 60,
    ),
  });

  static CacheableFile _folder(
    String name, {
    String? parent,
    required int daysAgo,
  }) {
    final base = parent == null ? '' : '/$parent';
    return CacheableFile(
      path: '$base/$name/',
      isDirectory: true,
      name: name,
      modifiedAt: _relative(daysAgo),
      createdAt: _relative(daysAgo + 30),
    );
  }

  static CacheableFile _file(
    String name, {
    String? parent,
    required int size,
    required int daysAgo,
  }) {
    final base = parent == null ? '' : '/$parent';
    return CacheableFile(
      path: '$base/$name',
      isDirectory: false,
      name: name,
      mimeType: _mimeFor(name),
      size: size,
      modifiedAt: _relative(daysAgo),
      createdAt: _relative(daysAgo + 30),
    );
  }

  static DateTime _relative(int daysAgo) =>
      DateTime.now().subtract(Duration(days: daysAgo, hours: 3));

  static String _mimeFor(String name) {
    final ext = name.split('.').last.toLowerCase();
    switch (ext) {
      case 'pdf':
        return 'application/pdf';
      case 'pptx':
        return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
      case 'docx':
        return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
      default:
        return 'application/octet-stream';
    }
  }
}
