CREATE TABLE [dbo].[Loan] (
	[Id]                 INT           IDENTITY (1, 1) NOT NULL,
	[LoanRequestId]		 INT NOT NULL,
	[LoanOfferId]		 INT NOT NULL,
	--[ProfileId]          INT           NOT NULL,
	[BorrowerId]         INT           NULL,
	[LenderId]           INT           NULL,
	[LoanAmount]         FLOAT (53)    NOT NULL,
	[FirstRepaymentDate] DATETIME      NOT NULL,
	[LoanDuration]       INT           NOT NULL,
	[TermTypeId]         INT NOT NULL,
	[LoanStatusId]       INT NOT NULL,
	[CreatedDate] DATETIME NOT NULL Default GETDATE(), 
	[InitialPaymentAmount] FLOAT NOT NULL DEFAULT 0,
	[DateAccepted] DATETIME  NULL Default GETDATE(), 
    [MonthlyInstallmentAmount] FLOAT NOT NULL DEFAULT 0, 
    [TotalPayment] FLOAT NOT NULL DEFAULT 0, 
    [Comment] NVARCHAR(MAX) NULL, 
    [RepayCompletedDate] DATETIME NULL, 
    CONSTRAINT [PK_Loan] PRIMARY KEY CLUSTERED ([Id] ASC),
	CONSTRAINT [FK_Loan_Lender] FOREIGN KEY ([LenderId]) REFERENCES [dbo].[Lender] ([Id]),
	CONSTRAINT [FK_Loan_Borrower] FOREIGN KEY ([BorrowerId]) REFERENCES [dbo].[Borrower] ([Id]),
	CONSTRAINT [FK_Loan_LoanRequest] FOREIGN KEY ([LoanRequestId]) REFERENCES [dbo].[LoanRequest]([Id]),
	CONSTRAINT [FK_Loan_LoanOffer] FOREIGN KEY ([LoanOfferId]) REFERENCES [dbo].[LoanOffer]([Id]),
	CONSTRAINT [FK_Loan_TermType] FOREIGN KEY ([TermTypeId]) REFERENCES [dbo].[TermType]([Id]),
	CONSTRAINT [FK_Loan_LoanStatus] FOREIGN KEY ([LoanStatusId]) REFERENCES [dbo].[LoanStatus]([Id])
);

