const { bool, number, object, string, array } = require('yup');

const RssModel = require('../RssModel');
const RELATIONSHIP_SOURCE = require('../../constant/relationship/source.constant');

const DEFAULT_CODE_YEAR = 2016;

class Building extends RssModel {
  constructor(payload = {}) {
    super(payload);
  }

  // eslint-disable-next-line class-methods-use-this
  schema() {
    return object().shape({
      id: string().required(),
      buildingKey: string().required(),
      name: string().min(1).max(128).trim().required().label('Name'),
      alternativeNames: array().of(string().trim()).ensure().label('Alternative Names'),
      code: string().max(32).trim().nullable().default(null).label('Code'),
      category: string().max(64).trim().nullable().default(null).label('Category'),
      ubccode: string().max(32).trim().nullable().default(null).label('Uniform Building Code'),
      assetNumber: string().max(32).trim().nullable().default(null).label('Asset Number'),
      primaryUsage: string().max(128).trim().nullable().default(null).label('Primary Usage'),
      address: string().max(128).trim().nullable().default(null).label('Address'),
      city: string().max(64).trim().nullable().default(null).label('City'),
      county: string().max(64).trim().nullable().default(null).label('County'),
      state: string().max(32).trim().nullable().default(null).label('State'),
      postalCode: string().max(32).trim().nullable().default(null).label('Postal Code'),
      size: number()
        .transform((value) => value || null)
        .nullable()
        .min(0, 'Size must be a positive number')
        .max(99999999999)
        .default(null)
        .label('Size'),
      codeYear: number()
        .required()
        .positive()
        .transform((value) => (Number.isInteger(value) ? parseInt(value, 10) : null))
        .integer()
        .max(9999)
        .default(DEFAULT_CODE_YEAR)
        .required()
        .label('Code Year'),
      fireSuppressionCoverage: bool().default(false).label('Fire Suppression Coverage'),
      source: string()
        .oneOf(Object.keys(RELATIONSHIP_SOURCE))
        .default(RELATIONSHIP_SOURCE.RSS)
        .required()
        .label('Source'),
    });
  }
}

module.exports = Building;
