import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '@config/database';
import { WorkflowRequest } from './WorkflowRequest';
import { User } from './User';

export enum IOStatus {
  PENDING = 'PENDING',
  BLOCKED = 'BLOCKED',
  RELEASED = 'RELEASED',
  CANCELLED = 'CANCELLED'
}

interface InternalOrderAttributes {
  ioId: string;
  requestId: string;
  ioNumber: string;
  ioRemark?: string;
  ioAvailableBalance?: number;
  ioBlockedAmount?: number;
  ioRemainingBalance?: number;
  organizedBy?: string;
  organizedAt?: Date;
  sapDocumentNumber?: string;
  status: IOStatus;
  createdAt: Date;
  updatedAt: Date;
}

interface InternalOrderCreationAttributes extends Optional<InternalOrderAttributes, 'ioId' | 'ioRemark' | 'ioAvailableBalance' | 'ioBlockedAmount' | 'ioRemainingBalance' | 'organizedBy' | 'organizedAt' | 'sapDocumentNumber' | 'status' | 'createdAt' | 'updatedAt'> { }

class InternalOrder extends Model<InternalOrderAttributes, InternalOrderCreationAttributes> implements InternalOrderAttributes {
  public ioId!: string;
  public requestId!: string;
  public ioNumber!: string;
  public ioRemark?: string;
  public ioAvailableBalance?: number;
  public ioBlockedAmount?: number;
  public ioRemainingBalance?: number;
  public organizedBy?: string;
  public organizedAt?: Date;
  public sapDocumentNumber?: string;
  public status!: IOStatus;
  public createdAt!: Date;
  public updatedAt!: Date;

  // Associations
  public request?: WorkflowRequest;
  public organizer?: User;
}

InternalOrder.init(
  {
    ioId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'io_id'
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      }
    },
    ioNumber: {
      type: DataTypes.STRING(50),
      allowNull: false,
      field: 'io_number'
    },
    ioRemark: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'io_remark'
    },
    ioAvailableBalance: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'io_available_balance'
    },
    ioBlockedAmount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'io_blocked_amount'
    },
    ioRemainingBalance: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'io_remaining_balance'
    },
    organizedBy: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'organized_by',
      references: {
        model: 'users',
        key: 'user_id'
      }
    },
    organizedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'organized_at'
    },
    sapDocumentNumber: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'sap_document_number'
    },
    status: {
      type: DataTypes.ENUM('PENDING', 'BLOCKED', 'RELEASED', 'CANCELLED'),
      defaultValue: 'PENDING',
      allowNull: false
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'updated_at'
    }
  },
  {
    sequelize,
    modelName: 'InternalOrder',
    tableName: 'internal_orders',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      {
        fields: ['request_id'],
        unique: false
      },
      {
        fields: ['io_number']
      },
      {
        fields: ['organized_by']
      }
    ]
  }
);

// Associations
InternalOrder.belongsTo(WorkflowRequest, {
  as: 'request',
  foreignKey: 'requestId',
  targetKey: 'requestId'
});

InternalOrder.belongsTo(User, {
  as: 'organizer',
  foreignKey: 'organizedBy',
  targetKey: 'userId'
});

export { InternalOrder };

