import Foundation
import Testing
import ForgejoKit
@testable import Forji

struct StateTests {

    // MARK: - IssueState

    @Test func issueStateOpenRawValue() {
        let state = IssueState(rawValue: "open")
        #expect(state == .open)
    }

    @Test func issueStateClosedRawValue() {
        let state = IssueState(rawValue: "closed")
        #expect(state == .closed)
    }

    @Test func issueStateUnknownReturnsNil() {
        let state = IssueState(rawValue: "unknown")
        #expect(state == nil)
    }

    @Test func issueStateEmptyReturnsNil() {
        let state = IssueState(rawValue: "")
        #expect(state == nil)
    }

    // MARK: - PullRequestState

    @Test func pullRequestStateOpenRawValue() {
        let state = PullRequestState(rawValue: "open")
        #expect(state == .open)
    }

    @Test func pullRequestStateClosedRawValue() {
        let state = PullRequestState(rawValue: "closed")
        #expect(state == .closed)
    }

    @Test func pullRequestStateUnknownReturnsNil() {
        let state = PullRequestState(rawValue: "unknown")
        #expect(state == nil)
    }

    // MARK: - Issue stateValue extension defaults to closed for unknown

    @Test func issueStateValueDefaultsToClosedForUnknown() {
        let issue = Issue(
            id: 1,
            number: 1,
            title: "Test",
            state: "unknown",
            user: User(id: 1, login: "test", avatarUrl: nil),
            createdAt: Date(),
            updatedAt: Date()
        )
        #expect(issue.stateValue == .closed)
    }

    @Test func issueStateValueReturnsOpenForOpen() {
        let issue = Issue(
            id: 1,
            number: 1,
            title: "Test",
            state: "open",
            user: User(id: 1, login: "test", avatarUrl: nil),
            createdAt: Date(),
            updatedAt: Date()
        )
        #expect(issue.stateValue == .open)
    }

    // MARK: - NotificationSubjectState

    @Test func notificationSubjectStateMerged() {
        let state = NotificationSubjectState(rawValue: "merged")
        #expect(state == .merged)
    }

    @Test func notificationSubjectStateUnknownReturnsNil() {
        let state = NotificationSubjectState(rawValue: "pending")
        #expect(state == nil)
    }

    // MARK: - IssueFilterState

    @Test func issueFilterStateAllRawValue() {
        let state = IssueFilterState(rawValue: "all")
        #expect(state == .all)
    }

    // MARK: - PullRequestFilterState

    @Test func pullRequestFilterStateAllRawValue() {
        let state = PullRequestFilterState(rawValue: "all")
        #expect(state == .all)
    }

    // MARK: - InvolvementScope

    @Test func involvementScopeInvolvedRawValue() {
        let scope = InvolvementScope(rawValue: "involved")
        #expect(scope == .involved)
    }

    @Test func involvementScopeCreatedRawValue() {
        let scope = InvolvementScope(rawValue: "created")
        #expect(scope == .created)
    }

    @Test func involvementScopeAssignedRawValue() {
        let scope = InvolvementScope(rawValue: "assigned")
        #expect(scope == .assigned)
    }

    @Test func involvementScopeMentionedRawValue() {
        let scope = InvolvementScope(rawValue: "mentioned")
        #expect(scope == .mentioned)
    }

    @Test func involvementScopeReviewRequestedRawValue() {
        let scope = InvolvementScope(rawValue: "review_requested")
        #expect(scope == .reviewRequested)
    }

    @Test func involvementScopeUnknownReturnsNil() {
        let scope = InvolvementScope(rawValue: "unknown")
        #expect(scope == nil)
    }

    @Test func involvementScopeAllCases() {
        #expect(InvolvementScope.allCases.count == 5)
    }
}
