import mongoose, { Schema } from 'mongoose';
import { IPropertyDoc, IPropertyModel } from './individualProperties.interface';
import { paginate, toJSON } from '@/shared/utils/plugins';

const basePropertySchema = new Schema(
  {
    // Basic Details
    title: { type: String, required: true, default: '', index: true },
    description: { type: String, default: '' },
    propertyType: { type: Schema.Types.ObjectId, ref: 'Category' },
    subcategory: {
      type: Schema.Types.ObjectId,
      ref: 'SubCategory',
      required: true,
      default: '',
    },
    listingType: {
      type: String,
      enum: ['sell', 'rent', 'lease', 'preLeased'],
      required: true,
      default: 'sell',
    },
    status: {
      type: String,
      enum: ['active', 'inactive', 'draft', 'sold'],
      default: 'active',
    },
    reraId: { type: String },
    configuration: { type: Schema.Types.ObjectId, ref: 'Configuration' },
    project: { type: Schema.Types.ObjectId, ref: 'Project' },
    propertyTags: [{ type: Schema.Types.ObjectId, ref: 'Tag' }],

    // Property Details
    carpetArea: { type: Number, default: '' },
    builtUpArea: { type: Number },
    superBuiltUpArea: { type: Number },
    length: { type: Number },
    width: { type: Number },
    bedrooms: { type: Number },
    bathrooms: { type: String },
    balconies: { type: String },
    totalFloors: { type: Number },
    floorNumber: { type: Number },
    carParking: { type: String },
    furnishingType: { type: String },
    ageOfProperty: { type: String },
    loadingArea: { type: String },
    possessionStatus: { type: String },
    category: { type: String },
    pantry: { type: String },
    parking: { type: String },
    washroom: { type: String },
    cabin: { type: String },

    // Location Details
    address: { type: String, default: '' },
    locality: { type: mongoose.Schema.Types.ObjectId, ref: 'Area' },
    city: { type: Schema.Types.ObjectId, ref: 'City' },
    state: { type: Schema.Types.ObjectId, ref: 'State' },
    pincode: { type: Number, default: 0 },
    facing: { type: String },
    latitude: { type: Number },
    longitude: { type: Number },
    placeId: { type: String, required: false },

    // Owner Details
    ownerName: { type: String, required: true, default: '' },
    ownerContact: { type: String, required: true, default: '' },
    ownerEmail: { type: String },

    // Additional Details
    amenities: [{ type: Schema.Types.ObjectId, ref: 'Amenities' }],
    tags: [{ type: String }],
    mediaUrls: [{ type: String }],
    source: [
      {
        sourceProjectId: { type: String, default: '' },
        id: { type: Schema.Types.ObjectId,  ref: 'Source' },
      },
    ],
    distance: { type: Number },
    facilities: { type: Map, of: Schema.Types.Mixed },

    // Sell Specific
    price: { type: Number },
    totalPrice: { type: Number },
    ownershipType: { type: String },
    brokerageAvailable: { type: String },
    brokerageAmount: { type: Number },
    availability: { type: String },

    // Rent/Lease Specific
    monthlyRent: { type: Number },
    securityDeposit: { type: Number },
    maintenanceCharges: { type: Number },
    lockInPeriod: { type: String },
    leaseDuration: { type: String },
    agreementType: { type: String },
    availableFrom: { type: Date },
    unitOfMeasurement: { type: String },
    // Pre-Leased Specific
    tenantName: { type: String },
    roiPercentage: { type: String },
    leaseStartDate: { type: Date },
    leaseEndDate: { type: Date },

    // Audit
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },

    companyId: {
      type: Schema.Types.ObjectId,
      ref: 'Company',
    },

    customFields: {
      type: Map,
      of: Schema.Types.Mixed,
      default: {},
    },
    isSharing: {
      type: Boolean,
      default: true,
    },
    flatNumber: { type: String },

    // Visibility fields
    visibleToUsers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
    visibleToTeams: [{ type: Schema.Types.ObjectId, ref: 'Team' }],
    isPublicVisibility: { type: Boolean, default: true },
  },
  {
    timestamps: true,
  },
).index({ title: 'text' });

basePropertySchema.plugin(toJSON);
basePropertySchema.plugin(paginate);

const individualProperties = mongoose.model<IPropertyDoc, IPropertyModel>(
  'individualProperties',
  basePropertySchema,
);

// const SellProperty = PrimaryProperty.discriminator(
//   'sell',
//   new Schema({
//     price: { type: String, required: true },
//     totalPrice: { type: String },
//     ownershipType: { type: String },
//     brokerageAvailable: { type: String },
//     brokerageAmount: { type: String },
//     availability: { type: String },
//   }),
// );

// // Pre-Leased property discriminator
// const PreLeasedProperty = PrimaryProperty.discriminator(
//   'preLease',
//   new Schema({
//     monthlyRent: { type: String, required: true },
//     tenantName: { type: String, required: true },
//     leaseDuration: { type: String, required: true },
//     lockInPeriod: { type: String, required: true },
//     roiPercentage: { type: String, required: true },
//     leaseStartDate: { type: Date, required: true },
//     leaseEndDate: { type: Date, required: true },
//   }),
// );

// // Rent property discriminator
// const RentProperty = PrimaryProperty.discriminator(
//   'rent',
//   new Schema({
//     monthlyRent: { type: String, required: true },
//     securityDeposit: { type: String, required: true },
//     maintenanceCharges: { type: String, required: true },
//     brokerageAvailable: { type: String },
//     brokerageAmount: { type: String },
//     lockInPeriod: { type: String, required: true },
//     leaseDuration: { type: String, required: true },
//     agreementType: { type: String, required: true },
//     availableFrom: { type: Date },
//   }),
// );

// // Lease property discriminator
// const LeaseProperty = PrimaryProperty.discriminator(
//   'lease',
//   new Schema({
//     monthlyRent: { type: String, required: true },
//     securityDeposit: { type: String, required: true },
//     maintenanceCharges: { type: String, required: true },
//     lockInPeriod: { type: String, required: true },
//     leaseDuration: { type: String, required: true },
//     agreementType: { type: String, required: true },
//   }),
// );

// export { SellProperty, RentProperty, LeaseProperty, PreLeasedProperty };
export default individualProperties;
