A federal judge Tuesday denied an effort by the National Rifle Association to file for bankruptcy protection, ruling that the gun rights group had filed the case in a bad-faith attempt to fend off a lawsuit by the New York attorney general.

“The Court finds, based on the totality of the circumstances, that the NRA’s bankruptcy petition was not filed in good faith but instead was filed as an effort to gain an unfair litigation advantage in the NYAG Enforcement Action and as an effort to avoid a regulatory scheme,” Judge Harlin Hale wrote in a 37-page decision.

The decision was a victory for New York Attorney General Letitia James, who filed a far-reaching civil suit against the group last August accusing top officials of fraud and self-dealing. NRA chief Wayne LaPierre and his legal team had contended that the lawsuit was a political act intended to destroy the organization.

“The NRA does not get to dictate when and where it will be held accountable,” James told reporters after the ruling. “This decision sends a loud and clear message that no one is above the law — not even one of the most powerful lobbying organizations in the country.”

LaPierre acknowledged the loss in a statement posted to the group’s Twitter account, but the embattled NRA leader vowed the group would continue to “confront our adversaries.”

“Although we are disappointed in some aspects of the decision, there is no change in the overall direction of our Association, its programs or its Second Amendment advocacy,” he said, adding: “The NRA will keep fighting as we’ve done for 150 years.”

Adam Levitin, who teaches bankruptcy law at Georgetown University Law Center, said the ruling was not surprising, calling the NRA’s petition “a poster child for a bad-faith filing.”

He said he did not think the organization had good arguments for appeal, noting that LaPierre’s position as head of the organization could be at risk in such a move.

Hale’s decision follows a weeks-long hearing that revealed details about alleged mismanagement and excessive spending by top officials at the influential gun lobby, including LaPierre, the NRA’s controversial leader for the past three decades.

Early this year, LaPierre announced that the nonprofit would file for bankruptcy and seek to move to Texas from New York state, where it was chartered in 1871, in the face of the suit filed by the New York attorney general.

In his decision Tuesday, Hale cited “lingering issues of secrecy and a lack of transparency” in the management of the nonprofit. But he said he was most troubled by “the surreptitious manner in which Mr. LaPierre obtained and exercised authority to file bankruptcy for the NRA,” noting that the NRA chief had not informed the majority of the board of directors, the chief financial officer or the general counsel.

However, the judge declined to appoint a trustee or independent examiner to oversee the group’s finances, saying that testimony during the hearing suggested the organization had improved its internal controls and “that the NRA now understands the importance of compliance.”

James’s office had argued that the NRA’s bid for bankruptcy was an attempt to duck serious allegations of wrongdoing. Several other parties, including the NRA’s longtime public relations firm, joined the attorney general’s office in opposing the petition or seeking independent oversight of the group.

In a surprise move at the end of closing arguments, a Justice Department lawyer who protects taxpayers’ interests in bankruptcy proceedings called for the rejection of the NRA’s petition or appointment of an independent trustee or an examiner.

Lisa Lambert, a lawyer with the U.S. trustee’s office, told the court that the evidence presented in the hearing showed that the nonprofit group lacked proper management and inappropriately paid for LaPierre’s personal expenses, including private jet travel and expensive suits from an exclusive Beverly Hills boutique.

After Hale’s ruling, opponents of the NRA said that the group’s attempt to seek bankruptcy protection only made it more vulnerable to legal scrutiny.

“Today’s disastrous decision for the NRA shows that they can’t even file for bankruptcy correctly, which doesn’t bode well for the many lawsuits and investigations they must now face,” John Feinblatt, president of Everytown for Gun Safety, a nonprofit group that advocates for gun control, said in a statement. “The NRA was forced to hang its dirty laundry out for the world to see, and has nothing to show for it but another stack of legal bills.”

Shannon Watts, founder of Moms Demand Action, said in a statement that “the testimony, evidence, and resolution of the trial have further damaged what little is left of the NRA’s reputation, painting the clearest picture yet of a non-profit organization that prioritized extravagant perks and insider payments to its executives at the expense of its rank-and-file members.”

Hale had several options in the case. He could have rejected the NRA bankruptcy petition, denying the group protection from the New York lawsuit. He could have allowed the bankruptcy to proceed while putting the group under the control of a court-supervised trustee and removing LaPierre from management. Or he could have appointed an examiner to review claims of NRA fiscal mismanagement.

Bankruptcy experts said that the evidence presented during the hearing made it difficult for the judge to allow the bankruptcy to proceed.

“The bankruptcy filing was a not too covert attempt to shield the NRA from the NY AG litigation and the Bankruptcy Court made it clear that it would not protect the NRA from New York’s regulatory power utilizing the Bankruptcy Code as a shield,” Eric Snyder, chair of the restructuring department of the Wilk Auslander law firm, said in an email.

The judge himself noted the unusual nature of the case last week, asking participants during the trial’s final arguments whether they thought the bankruptcy code permits a financially strong organization to file for bankruptcy to protect itself from actions taken in the public interest by another court.

Lawyers for the NRA acknowledged what one described as “cringeworthy” activities by NRA officials in the past but argued that management improvements had been instituted effectively.

However, a group of dissident members of the NRA board intervened in the case, saying most of the board and top organization officials were not informed in advance of LaPierre’s plan to file for bankruptcy and move to Texas. Some of them sought the appointment of an independent examiner to review the group’s financial management.

One of them, Phillip Journey, who is also a Kansas state judge, said that while he opposed the way in which the NRA filed for bankruptcy, he did not want the petition to be dismissed, saying the group “would probably die the death of a thousand cuts” if it had to face the New York and other lawsuits.

During the last day of the trial, the NRA submitted a formal plan of reorganization that had been approved only the day before at a special board meeting. The new plan called for adding a top compliance officer to the organization but otherwise kept the current management — including LaPierre — in place.

LaPierre testified twice during the trial, which was conducted via Webex video conference. He acknowledged that he should have disclosed some of the lavish benefits he received and that he clashed with many former executives and associates, including the leaders of Ackerman McQueen, the NRA’s longtime public relations firm, which claims the NRA owes it more than $1 million. But he said past financial and management abuses had been corrected.

After the ruling, Ackerman McQueen’s executive vice president, Bill Powers, said the case “underscores the incompetence and failure of NRA leadership and its legal team.” Ackerman and others previously affiliated with the NRA cited tens of millions in legal bills submitted over the past three years by the NRA’s outside legal counsel, William Brewer III.

The 11 days of hearings in the case put a spotlight on the practices inside the NRA at a key political juncture in the fight over gun control.

Congress is considering new legislation following a rash of mass shootings in April. In addition, the Supreme Court has agreed to hear an NRA-backed lawsuit challenging the constitutionality of state restrictions on carrying concealed weapons.

In the past decade, the NRA reacted with defiance to gun-control proposals that emerged after mass shootings, including in 2012 when LaPierre gave a speech calling for armed guards at schools in the aftermath of the attack at Sandy Hook Elementary School in Newtown, Conn., in which 26 were killed. The NRA effectively blocked what at first appeared to be a rare bipartisan demand for congressional regulatory action after the school massacre.