//
//  LotteryTicket+Extensions.swift
//  LotteryTracker
//
//  Created by aaa on 2026/1/21.
//

import Foundation
internal import CoreData

// 添加通知名称
extension Notification.Name {
    static let ticketsUpdated = Notification.Name("ticketsUpdated")
    static let appReset = Notification.Name("appReset")
}

// 彩票类型枚举
enum LotteryType: String, CaseIterable, Identifiable {
    case doubleColorBall = "双色球"
    case superLotto = "大乐透"
    case happy8 = "快乐八"
    case sevenStar = "七星彩"
    case other = "其他"
    
    var id: String { rawValue }
}

// 票务状态枚举
enum TicketStatus: String {
    case pending = "待开奖"
    case won = "已中奖"
    case lost = "未中奖"
}

// 定义快乐8类型枚举
enum Happy8Type: Int, CaseIterable, Identifiable {
    case happy8One = 1
    case happy8Two = 2
    case happy8Three = 3
    case happy8Four = 4
    case happy8Five = 5
    case happy8Six = 6
    case happy8Seven = 7
    case happy8Eight = 8
    case happy8Nine = 9
    case happy8Ten = 10
    
    var id: Int { rawValue }
    
    var string: String {
        let chineseNumbers = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
        return "选\(chineseNumbers[rawValue - 1])"
    }
    
    var displayName: String {
        "选\(rawValue)" // 备用显示
    }
}

// 扩展 LotteryTicket 类
extension LotteryTicket {
    // 计算属性：获取彩票类型
    var lotteryType: LotteryType {
        get {
            LotteryType(rawValue: type ?? "") ?? .other
        }
        set {
            type = newValue.rawValue
        }
    }
    
    // 计算属性：获取票务状态
    var ticketStatus: TicketStatus {
        get {
            TicketStatus(rawValue: status ?? "") ?? .pending
        }
        set {
            status = newValue.rawValue
        }
    }
    
    // 计算属性：计算盈亏
    var profit: Float {
        return prizeAmount - amount
    }
    
    // 计算属性：格式化日期
    var formattedDate: String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter.string(from: date ?? Date())
    }
    
    // 计算属性：格式化开奖日期
    var formattedDrawDate: String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter.string(from: drawDate ?? Date())
    }
    
    // 计算属性：获取号码数组
    var numberArray: [String] {
        guard let numbers = numbers, !numbers.isEmpty else {
            return []
        }
        
        // 按空格或逗号分割
        let components = numbers.components(separatedBy: CharacterSet(charactersIn: " "))
        return components.filter { !$0.isEmpty }
    }
}

// 扩展 FetchRequest
extension LotteryTicket {
    // 获取所有记录的请求
    static func fetchAllTickets() -> NSFetchRequest<LotteryTicket> {
        let request: NSFetchRequest<LotteryTicket> = LotteryTicket.fetchRequest()
        request.sortDescriptors = [
            NSSortDescriptor(keyPath: \LotteryTicket.date, ascending: false)
        ]
        return request
    }
    
    // 获取待开奖记录的请求
    static func fetchPendingTickets() -> NSFetchRequest<LotteryTicket> {
        let request: NSFetchRequest<LotteryTicket> = LotteryTicket.fetchRequest()
        request.predicate = NSPredicate(format: "status == %@", TicketStatus.pending.rawValue)
        request.sortDescriptors = [
            NSSortDescriptor(keyPath: \LotteryTicket.drawDate, ascending: true)
        ]
        return request
    }
}

