import 'package:drift/drift.dart';
import 'converters/json_converter.dart';

@DataClassName('UserCollection')
class Users extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get serverId => text().nullable().unique()();

  TextColumn get email => text().withDefault(const Constant(''))();
  IntColumn get xp => integer().withDefault(const Constant(0))();
  IntColumn get level => integer().withDefault(const Constant(1))();
  RealColumn get currentBodyweight =>
      real().withDefault(const Constant(70.0))();
  TextColumn get exerciseVariants =>
      text().map(const MapConverter()).nullable()();

  TextColumn get inventorySettings =>
      text().map(const MapConverter()).nullable()();
  TextColumn get avatarConfig => text().map(const MapConverter()).nullable()();

  DateTimeColumn get lastSyncAt => dateTime().nullable()();
  BoolColumn get isDirty => boolean().withDefault(const Constant(false))();

  DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
  DateTimeColumn get updatedAt => dateTime().withDefault(currentDateAndTime)();
}

@DataClassName('CycleCollection')
class Cycles extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get serverId => text().nullable().unique()();

  TextColumn get userId => text()();

  IntColumn get cycleNumber => integer()();
  DateTimeColumn get startDate => dateTime()();
  DateTimeColumn get endDate => dateTime().nullable()();
  BoolColumn get isActive => boolean().withDefault(const Constant(true))();

  TextColumn get trainingMaxes => text().map(const MapConverter())();

  BoolColumn get isDirty => boolean().withDefault(const Constant(false))();
  DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
  DateTimeColumn get updatedAt => dateTime().withDefault(currentDateAndTime)();
}

@DataClassName('WorkoutCollection')
class Workouts extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get serverId => text().nullable().unique()();

  TextColumn get userId => text()();
  TextColumn get cycleId => text()();

  IntColumn get week => integer()();
  IntColumn get day => integer()();

  DateTimeColumn get scheduledDate => dateTime().nullable()();
  DateTimeColumn get completedAt => dateTime().nullable()();
  IntColumn get xpEarned => integer().withDefault(const Constant(0))();

  TextColumn get exercises => text().map(const ListConverter())();
  TextColumn get notes => text().withDefault(const Constant(''))();

  BoolColumn get isDirty => boolean().withDefault(const Constant(false))();
  DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
  DateTimeColumn get updatedAt => dateTime().withDefault(currentDateAndTime)();
}

@DataClassName('QuestCollection')
class Quests extends Table {
  TextColumn get id => text()();

  TextColumn get type => text()(); // 'daily', 'milestone', 'story'
  TextColumn get title => text()();
  TextColumn get description => text()();

  IntColumn get targetValue => integer()();
  IntColumn get currentValue => integer().withDefault(const Constant(0))();

  IntColumn get rewardXP => integer()();
  TextColumn get rewardItem => text().nullable()();

  BoolColumn get isCompleted => boolean().withDefault(const Constant(false))();
  BoolColumn get isClaimed => boolean().withDefault(const Constant(false))();

  DateTimeColumn get expiresAt => dateTime().nullable()();
  DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();

  @override
  Set<Column> get primaryKey => {id};
}
