import {
  Company,
  CompanyType,
  CompanyStatus,
  CompanyNationality,
} from '../entities/company.entity';
import {
  CompanyProfile,
  ProfileLicenseFileView,
} from '../entities/company-profile.entity';
import { ResponseExternalProfileDto } from './response-external-profile.dto';

export class ResponseCompanyProfileDto {
  id: string;
  companyId: string;
  type: string;
  reference: string;
  status: string;
  /** @deprecated Superseded by licenseFiles. Kept for back-compat. */
  businessLicense?: string | null;
  /**
   * Business-license documents (FileRecord-backed) with review state. Left empty
   * by the constructor and populated asynchronously by the controller, since the
   * files and their pending-change status require DB lookups.
   */
  licenseFiles: ProfileLicenseFileView[];
  attributes?: Record<string, any> | null;
  /** Reviewer note when the role is rejected (drives the reapply prompt). */
  reviewNote?: string | null;
  createdAt: Date;
  updatedAt: Date;

  constructor(profile: CompanyProfile) {
    this.id = profile.id;
    this.companyId = profile.companyId;
    this.type = profile.type;
    this.reference = profile.reference ?? '';
    this.status = profile.status;
    this.businessLicense = profile.businessLicense;
    this.licenseFiles = [];
    this.attributes = profile.attributes;
    this.reviewNote = profile.reviewNote ?? null;
    this.createdAt = profile.createdAt;
    this.updatedAt = profile.updatedAt;
  }
}

export class ResponseCompanyDto {
  id: string;
  name: string;
  type: CompanyType;
  status: CompanyStatus;
  nationality?: CompanyNationality | null;
  tin: string;
  vatNumber?: string | null;
  fanNumber?: string | null;
  country: string;
  address?: string | null;
  phone?: string | null;
  email?: string | null;
  website?: string | null;
  attributes?: Record<string, any> | null;
  profiles?: ResponseExternalProfileDto[];
  companyProfiles?: ResponseCompanyProfileDto[];
  /**
   * Whether the owning portal user has submitted the onboarding wizard.
   * Approval decisions are blocked while this is false. Staff-created
   * companies (no external profiles) count as completed. Undefined when the
   * external profiles weren't loaded.
   */
  onboardingCompleted?: boolean;
  createdAt: Date;
  updatedAt: Date;

  constructor(company: Company) {
    this.id = company.id;
    this.name = company.name;
    this.type = company.type;
    this.status = company.status;
    this.nationality = company.nationality ?? null;
    this.tin = company.tin;
    this.vatNumber = company.vatNumber;
    this.fanNumber = company.fanNumber;
    this.country = company.country;
    this.address = company.address;
    this.phone = company.phone;
    this.email = company.email;
    this.website = company.website;
    this.attributes = company.attributes;
    this.profiles = company.profiles?.map((p) => new ResponseExternalProfileDto(p));
    this.companyProfiles = company.companyProfiles?.map(
      (p) => new ResponseCompanyProfileDto(p),
    );
    this.onboardingCompleted = company.profiles
      ? company.profiles.length === 0 ||
        company.profiles.some((p) => p.onboardingCompleted)
      : undefined;
    this.createdAt = company.createdAt;
    this.updatedAt = company.updatedAt;
  }
}
