import 'dart:convert';
import 'package:hive_ce/hive.dart';
import 'package:worker/core/constants/storage_constants.dart';
import 'package:worker/core/database/models/enums.dart';

part 'design_request_model.g.dart';

@HiveType(typeId: HiveTypeIds.designRequestModel)
class DesignRequestModel extends HiveObject {
  DesignRequestModel({
    required this.requestId,
    required this.userId,
    required this.projectName,
    required this.projectType,
    required this.area,
    required this.style,
    required this.budget,
    required this.currentSituation,
    required this.requirements,
    this.notes,
    this.attachments,
    required this.status,
    this.assignedDesigner,
    this.finalDesignLink,
    this.feedback,
    this.rating,
    this.estimatedCompletion,
    required this.createdAt,
    this.completedAt,
    this.updatedAt,
  });

  @HiveField(0)
  final String requestId;
  @HiveField(1)
  final String userId;
  @HiveField(2)
  final String projectName;
  @HiveField(3)
  final ProjectType projectType;
  @HiveField(4)
  final double area;
  @HiveField(5)
  final String style;
  @HiveField(6)
  final double budget;
  @HiveField(7)
  final String currentSituation;
  @HiveField(8)
  final String requirements;
  @HiveField(9)
  final String? notes;
  @HiveField(10)
  final String? attachments;
  @HiveField(11)
  final DesignStatus status;
  @HiveField(12)
  final String? assignedDesigner;
  @HiveField(13)
  final String? finalDesignLink;
  @HiveField(14)
  final String? feedback;
  @HiveField(15)
  final int? rating;
  @HiveField(16)
  final DateTime? estimatedCompletion;
  @HiveField(17)
  final DateTime createdAt;
  @HiveField(18)
  final DateTime? completedAt;
  @HiveField(19)
  final DateTime? updatedAt;

  factory DesignRequestModel.fromJson(Map<String, dynamic> json) =>
      DesignRequestModel(
        requestId: json['request_id'] as String,
        userId: json['user_id'] as String,
        projectName: json['project_name'] as String,
        projectType: ProjectType.values.firstWhere(
          (e) => e.name == json['project_type'],
        ),
        area: (json['area'] as num).toDouble(),
        style: json['style'] as String,
        budget: (json['budget'] as num).toDouble(),
        currentSituation: json['current_situation'] as String,
        requirements: json['requirements'] as String,
        notes: json['notes'] as String?,
        attachments: json['attachments'] != null
            ? jsonEncode(json['attachments'])
            : null,
        status: DesignStatus.values.firstWhere((e) => e.name == json['status']),
        assignedDesigner: json['assigned_designer'] as String?,
        finalDesignLink: json['final_design_link'] as String?,
        feedback: json['feedback'] as String?,
        rating: json['rating'] as int?,
        estimatedCompletion: json['estimated_completion'] != null
            ? DateTime.parse(json['estimated_completion']?.toString() ?? '')
            : null,
        createdAt: DateTime.parse(json['created_at']?.toString() ?? ''),
        completedAt: json['completed_at'] != null
            ? DateTime.parse(json['completed_at']?.toString() ?? '')
            : null,
        updatedAt: json['updated_at'] != null
            ? DateTime.parse(json['updated_at']?.toString() ?? '')
            : null,
      );

  Map<String, dynamic> toJson() => {
    'request_id': requestId,
    'user_id': userId,
    'project_name': projectName,
    'project_type': projectType.name,
    'area': area,
    'style': style,
    'budget': budget,
    'current_situation': currentSituation,
    'requirements': requirements,
    'notes': notes,
    'attachments': attachments != null ? jsonDecode(attachments!) : null,
    'status': status.name,
    'assigned_designer': assignedDesigner,
    'final_design_link': finalDesignLink,
    'feedback': feedback,
    'rating': rating,
    'estimated_completion': estimatedCompletion?.toIso8601String(),
    'created_at': createdAt.toIso8601String(),
    'completed_at': completedAt?.toIso8601String(),
    'updated_at': updatedAt?.toIso8601String(),
  };
}
