import 'package:drift/drift.dart';
import 'package:niogu_app/core/database/tables/customers.dart';
import 'package:niogu_app/core/database/tables/outlets.dart';
import 'package:niogu_app/core/database/tables/users.dart';
import 'package:niogu_app/core/utils/string_map_converter.dart';
import 'package:uuid/uuid.dart';

class Orders extends Table {
  TextColumn get localId => text().clientDefault(() => const Uuid().v7().toString())();
  TextColumn get serverId => text().nullable()();
  TextColumn get orderNumber => text().unique()();
  TextColumn get outletId => text().references(Outlets, #localId)();
  TextColumn get outletNameSnapshot => text()();
  TextColumn get customerId => text().references(Customers, #localId).nullable()();
  TextColumn get customerNameSnapshot => text().nullable()();
  TextColumn get customerEmailSnapshot => text().nullable()();
  TextColumn get customerPhoneNumberSnapshot => text().nullable()();
  TextColumn get userId => text().references(Users, #localId)();
  TextColumn get adminNameSnapshot => text()();
  TextColumn get source => text()();
  BoolColumn get alreadyRead => boolean().nullable()();
  TextColumn get orderStatus => text()();
  BoolColumn get isCancellation => boolean().nullable()();
  BoolColumn get cancellationAccepted => boolean().nullable()();
  TextColumn get approvalProccess => text().nullable()();
  TextColumn get canceledBy => text().nullable()();
  TextColumn get deliveryType => text().nullable()();
  TextColumn get deliveryPrefrence => text().nullable()();
  RealColumn get shippingCost => real().withDefault(const Constant(0.0))();
  RealColumn get totalOrder => real().withDefault(const Constant(0.0))();
  RealColumn get discount => real().withDefault(const Constant(0.0))();
  RealColumn get tax => real().withDefault(const Constant(0.0))();
  RealColumn get totalAmount => real().withDefault(const Constant(0.0))();
  RealColumn get amountPaid => real().withDefault(const Constant(0.0))();
  RealColumn get changeAmount => real().withDefault(const Constant(0.0))();
  TextColumn get notes => text().nullable()();
  TextColumn get paymentProofPath => text().nullable()();
  TextColumn get paymentProofUrl => text().nullable()();
  TextColumn get paymentMethod => text().nullable()();
  TextColumn get customerAddressSnapshot => text().map(StringMapConverter()).nullable()();
  TextColumn get syncStatus => text().withDefault(const Constant('dirty'))();
  DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
  DateTimeColumn get updatedAt => dateTime().withDefault(currentDateAndTime)();

  @override
  // TODO: implement primaryKey
  Set<Column<Object>>? get primaryKey => {localId};
}