import 'package:drift/drift.dart' hide JsonKey;
import 'package:json_annotation/json_annotation.dart';
import 'package:niogu_app/core/database/app_database.dart';
import 'package:niogu_app/core/enums/sync_status.dart';

part 'tenant_model.g.dart';

@JsonSerializable()
class TenantModel {
  @JsonKey(name: 'id')
  final String serverId;

  @JsonKey(name: 'business_code')
  final String businessCode;
  
  @JsonKey(name: 'business_name')
  final String businessName;
  
  @JsonKey(name: 'business_email')
  final String? businessEmail;
  
  @JsonKey(name: 'business_phone')
  final String? businessPhone;
  
  @JsonKey(name: 'business_type')
  final String? businessType;
  
  @JsonKey(name: 'server_logo_url')
  final String? serverLogoUrl;
  
  final String? subdomain;
  
  @JsonKey(name: 'online_store_name')
  final String? onlineStoreName;
  
  @JsonKey(name: 'online_open_time')
  final String? onlineOpenTime;
  
  @JsonKey(name: 'online_close_time')
  final String? onlineCloseTime;
  
  @JsonKey(name: 'is_close_service')
  final bool? isCloseService;
  
  @JsonKey(name: 'preparation_time_minutes')
  final int? preparationTimeMinutes;
  
  @JsonKey(name: 'order_interval_minutes')
  final int? orderIntervalMinutes;
  
  @JsonKey(name: 'allow_cod')
  final bool? allowCod;
  
  @JsonKey(name: 'max_delivery_radius_km')
  final double? maxDeliveryRadiusKm;
  
  @JsonKey(name: 'delivery_fee_type')
  final String? deliveryFeeType;
  
  @JsonKey(name: 'delivery_flat_fee')
  final double? deliveryFlatFee;
  
  @JsonKey(name: 'delivery_fee_per_km')
  final double? deliveryFeePerKm;
  
  @JsonKey(name: 'delivery_base_fee')
  final double? deliveryBaseFee;
  
  @JsonKey(name: 'review_display_type')
  final String? reviewDisplayType;
  
  @JsonKey(name: 'review_display_policy')
  final String? reviewDisplayPolicy;
  
  @JsonKey(name: 'subscription_level')
  final String? subscriptionLevel;
  
  @JsonKey(name: 'limit_outlet')
  final int? limitOutlet;
  
  @JsonKey(name: 'subscription_expires_at')
  final DateTime? subscriptionExpiresAt;
  
  @JsonKey(name: 'created_at')
  final DateTime createdAt;

  @JsonKey(name: 'updated_at')
  final DateTime updatedAt;

  const TenantModel({
    required this.serverId,
    required this.businessCode,
    required this.businessName,
    this.businessEmail,
    this.businessPhone,
    this.businessType,
    this.serverLogoUrl,
    this.subdomain,
    this.onlineStoreName,
    this.onlineOpenTime,
    this.onlineCloseTime,
    this.isCloseService,
    this.preparationTimeMinutes,
    this.orderIntervalMinutes,
    this.allowCod,
    this.maxDeliveryRadiusKm,
    this.deliveryFeeType,
    this.deliveryFlatFee,
    this.deliveryFeePerKm,
    this.deliveryBaseFee,
    this.reviewDisplayType,
    this.reviewDisplayPolicy,
    this.subscriptionLevel,
    this.limitOutlet,
    this.subscriptionExpiresAt,
    required this.createdAt,
    required this.updatedAt
  });

  factory TenantModel.fromJson(Map<String, dynamic> json) => _$TenantModelFromJson(json);
  Map<String, dynamic> toJson() => _$TenantModelToJson(this);

  TenantsCompanion toDriftCompanion({String? localLogoPath}) {
    return TenantsCompanion(
      localId: Value(serverId),
      serverId: Value(serverId),
      businessCode: Value(businessCode),
      businessName: Value(businessName),
      businessEmail: Value(businessEmail),
      businessPhone: Value(businessPhone),
      businessType: Value(businessType),
      serverLogoUrl: Value(serverLogoUrl),
      localLogoPath: Value(localLogoPath),
      subdomain: Value(subdomain),
      onlinStoreName: Value(onlineStoreName),
      onlineOpenTime: Value(onlineOpenTime),
      onlineCloseTime: Value(onlineCloseTime),
      isCloseService: Value(isCloseService ?? false),
      preparationTimeMinutes: Value(preparationTimeMinutes ?? 0),
      orderIntervalMinutes: Value(orderIntervalMinutes ?? 0),
      allowCod: Value(allowCod ?? false),
      maxDeliveryRadiusKm: Value(maxDeliveryRadiusKm ?? 0.0),
      deliveryFeeType: Value(deliveryFeeType ?? 'fixed'),
      deliveryFlatFee: Value(deliveryFlatFee ?? 0.0),
      deliveryFeePerKm: Value(deliveryFeePerKm ?? 0.0),
      deliveryBaseFee: Value(deliveryBaseFee ?? 0.0),
      reviewDisplayType: Value(reviewDisplayType ?? 'rating_only'),
      reviewDisplayPolicy: Value(reviewDisplayPolicy ?? 'four_and_five_star_only'),
      subscriptionLevel: Value(subscriptionLevel ?? 'standart'),
      limitOutlet: Value(limitOutlet ?? 5),
      subscriptionExpiresAt: Value(subscriptionExpiresAt),
      syncStatus: Value(SyncStatus.synced.status),
      createdAt: Value(createdAt),
      updatedAt: Value(updatedAt)
    );
  }
}