// ignore_for_file: recursive_getters

// Dart imports:
import 'dart:convert';
import 'dart:ffi';

// Package imports:
import 'package:drift/drift.dart';
import 'package:uuid/uuid.dart';

///
/// Примесь для добавления основного ключа в виде текста
/// Для хранения uuid
///
mixin _UuidPrimaryKey on Table {
  @override
  // ignore: always_specify_types
  Set<Column>? get primaryKey => <TextColumn>{id};

  /// Идентификатор записи
  TextColumn get id =>
      text()
          .withLength(min: 36, max: 36)
          .clientDefault(() => const Uuid().v6())();
}

///
/// Примесь для добавления полей даты
///
mixin _Timestampable on Table {
  /// Дата создания
  DateTimeColumn get createdAt =>
      dateTime()
          .named('created_at')
          .check(createdAt.isBiggerThan(Constant<DateTime>(DateTime(1950))))
          .withDefault(currentDateAndTime)();

  /// Дата последней модификации
  DateTimeColumn get updatedAt =>
      dateTime()
          .named('updated_at')
          .check(updatedAt.isBiggerThan(Constant<DateTime>(DateTime(1950))))
          .withDefault(currentDateAndTime)();
}

///
/// Примесь для добавления полей даты удаления
///
mixin _Deletable on Table {
  /// Дата удаления
  DateTimeColumn get deletedAt =>
      dateTime()
          .named('deleted_at')
          .nullable()
          .check(deletedAt.isBiggerThan(Constant<DateTime>(DateTime(1950))))();
}

/// -- Таблицы --

///
///
///
@DataClassName('Collection')
class Collections extends Table with _UuidPrimaryKey, _Timestampable {
  TextColumn get title => text()();
  TextColumn get desc => text()();
  BlobColumn get image => blob().nullable()();
  TextColumn get payload => text().nullable()();
  IntColumn get likesCount => integer().withDefault(Constant(0))();
  BoolColumn get isLiked => boolean().withDefault(Constant(false))();
  BoolColumn get isPublic => boolean().withDefault(Constant(false))();
  BoolColumn get includeInTraining => boolean().withDefault(Constant(false))();
}

///
///
///
@DataClassName('Ticket')
class Tickets extends Table with _UuidPrimaryKey, _Timestampable {
  TextColumn get question => text()();
  TextColumn get answer => text()();
  BlobColumn get image => blob().nullable()();
  TextColumn get collectionId =>
      text().references(Collections, #id, onDelete: KeyAction.cascade)();
  RealColumn get progress => real().withDefault(Constant(0))();
}
