const DataSource = require('loopback-datasource-juggler').DataSource;

const modelList = [
  {
    name: 'Test',
    view: false,
    schema: '',
  },
  {
    name:'Schema',
    view: false,
    schema: 'aSchema'
  },
  {
    name:'View',
    view: true,
    schema: ''
  },
    {
    name:'Naming',
    view: true,
    schema: 'Naming'
  },
    {
    name:'Doctor',
    view: false,
    schema: 'Doctor'
  },
    {
    name:'Patient',
    view: false,
    schema: 'Patient'
  },
    {
    name:'Appointment',
    view: false,
    schema: 'Appointment'
  }
];
// In real model definitions, the schema is contained in options->connectorName->schema
const fullDefinitions = [
  {
    'name': 'Schema',
    'schema': 'aSchema',
    'properties': {}
  },
  {
    'name': 'View',
    'view': true,
    'schema': '',
    'properties': {}
  },
  {
    'name': 'Test',
    'properties': {
      'dateTest': {
        'type': 'Date',
        'required': false,
        'length': null,
        'precision': null,
        'scale': null,
      },
      'numberTest': {
        'type': 'Number',
        'required': false,
        'length': null,
        'precision': null,
        'scale': null,
      },
      'stringTest': {
        'type': 'String',
        'required': false,
        'length': null,
        'precision': null,
        'scale': null,
      },
      'booleanText': {
        'type': 'Boolean',
        'required': false,
        'length': null,
        'precision': null,
        'scale': null,
      },
      'id': {
        'type': 'Number',
        'length': null,
        'precision': null,
        'scale': 0,
        'id': 1,
      },
      'isActive': {
        'type': 'boolean',
        'required': false,
        'length': null,
        'precision': null,
        'scale': null,
      },
    },
  },
    {
    'name': 'Naming',
    'schema': 'Naming',
    'properties': {
      'ID': {
        'type': 'Number',
        'id': 1,
        'required': true,
        'length': null,
        'precision': null,
        'scale': null,
      },
      'snake_case': {
        'type': 'Number',
        'required': false,
        'length': null,
        'precision': null,
        'scale': null,
      },
      'lowercase': {
        'type': 'Boolean',
        'required': false,
        'length': null,
        'precision': null,
        'scale': null,
      },
      'camelCase': {
        'type': 'Number',
        'required': false,
        'length': null,
        'precision': null,
        'scale': null,
      },
    },
  },
    {
    'name': 'Doctor',
    'properties': {
      'id': {
        'type': 'Number',
        'length': null,
        'precision': null,
        'required': false,
        'scale': 0,
      'id': 1,
      },
      'name': {
        'type': 'String',
        'required': false,
        'length': 45,
        'precision': null,
        'scale': null,
      },
    },
  },
  {
    'name': 'Patient',
    'properties': {
      'pid': {
        'type': 'Number',
        'length': null,
        'precision': null,
        'required': false,
        'scale': 0,
        'id': 1,
      },
      'name': {
        'type': 'String',
        'required': false,
        'length': 45,
        'precision': null,
        'scale': null,
      },
    },
  },
  {
    name: 'Appointment',
    className: 'Appointment',
    modelBaseClass: 'Entity',
    isModelBaseBuiltin: true,
    options: {
      relations: {
        doctorIdRel: { model: 'Doctor', type: 'belongsTo', foreignKey: 'doctorId' },
        patientIdRel: { model: 'Patient', type: 'belongsTo', foreignKey: 'patientId' }
      }
    },
    properties: {
      id: {
        type: "number",
        precision: 10,
        scale: 0,
        generated: 1,
        id: 1,
        mysql: "{columnName: 'id', dataType: 'int', dataLength: null, dataPrecision: 10, dataScale: 0, nullable: 'N', generated: 1}",
        tsType: 'number'
      },
      patientId: {
        type: "number",
        precision: 10,
        scale: 0,
        generated: 0,
        mysql: "{columnName: 'patientId', dataType: 'int', dataLength: null, dataPrecision: 10, dataScale: 0, nullable: 'Y', generated: 0}",
        tsType: 'number'
      },
      doctorId: {
        type: "number",
        precision: 10,
        scale: 0,
        generated: 0,
        mysql: "{columnName: 'doctorId', dataType: 'int', dataLength: null, dataPrecision: 10, dataScale: 0, nullable: 'Y', generated: 0}",
        tsType: 'number'
      }
    },
    allowAdditionalProperties: true,
    modelSettings: {
      settings: {
        idInjection: false,
        relations: {
          doctorIdRel: {model: 'Doctor', type: 'belongsTo', foreignKey: 'doctorId'},
          patientIdRel: {model: 'Patient', type: 'belongsTo', foreignKey: 'patientId'}
        }
      }
    }
  },
];

class DiscoverOnly extends DataSource {
  constructor() {
    super();
    this.name = 'mem';
    this.connected = true;
  }

  disconnect() {
    this.connected = false;
  }

  async discoverModelDefinitions(options = {views: true}) {
    let models = modelList;
    if (!options.views) {
      models = models.filter(m => !m.view);
    }
    if (options.schema) {
      models = models.filter(m => m.schema === options.schema);
    }

    return models;
  }

  async discoverSchema(name, options = {schema:''}) {
    let fullDefs = fullDefinitions;
    if (options.schema) {
      fullDefs = fullDefs.filter(d => d.schema === options.schema);
    }
    return fullDefs.find(d => d.name === name);
  }
}
module.exports  = {
  DiscoverOnly
};
