import  {createLogger,transports, format}  from 'winston';
import { createClient } from 'redis';
import { Repository, Schema, SchemaDefinition } from 'redis-om';
const baseListingSchemaDef : SchemaDefinition = {
    id: { type: 'string' },
    userId: { type: 'string' },
    listingsCategory: { type: 'string' },    
    title: { type: 'string' },
    description: { type: 'string' },
    country: { type: 'string' },
    state:{ type: 'string' },    
    city:{ type: 'string' },
    zipCode: { type: 'number' },
    type: { type: 'string' },
    price: { type: 'number' },
    favoritesForUser:{ type: 'string[]' },
    hideImage:{ type: 'boolean' },
    draft:{ type: 'boolean' },
    created:{ type: 'date' },
    updated:{ type: 'date' }
  }
const businessListingSchemaDef : SchemaDefinition = {
    ...baseListingSchemaDef,
    salesRevenue: { type: 'number' },    
    cashFlow: { type: 'number' },    
    employees: { type: 'number' },    
    established: { type: 'number' },    
    internalListingNumber: { type: 'number' },    
    realEstateIncluded:{ type: 'boolean' },
    leasedLocation:{ type: 'boolean' },
    franchiseResale:{ type: 'boolean' },
    supportAndTraining: { type: 'string' },
    reasonForSale: { type: 'string' },
    brokerLicencing: { type: 'string' },
    internals: { type: 'string' },
}
const businessListingSchema = new Schema('business',businessListingSchemaDef, {
    dataStructure: 'JSON'
})
const logger = createLogger({
    transports: [
      new transports.Console(),
    ],
    format: format.combine(
        format.colorize(),
        format.timestamp(),
        format.printf(({ timestamp, level, message, service }) => {
          return `[${timestamp}] ${service} ${level}: ${message}`;
        })
      ),
});
const redis = await createClient()
.on('error', err => console.log('Redis Client Error', err))
.connect();
const businessListingRepository = new Repository(businessListingSchema, redis);

(async () => {
    logger.info(`start findBusinessListings:`);
    const result = await redis.ft.search('business:index','*',{LIMIT:{from:0,size:200}});
    //result.documents.forEach(d=>console.log(d));
    logger.info(`end findBusinessListings:`);
    
    logger.info(`start businessListingRepository:`);
    await businessListingRepository.search().return.all()
    logger.info(`end businessListingRepository:`);

    redis.disconnect();
})();