package dev.banking.model;

import java.util.concurrent.locks.*;

public class BankAccount {

    private final int accountId;
    private long balance;

    /*
     * Students may introduce additional fields
     * such as:
     * - Lock / ReentrantLock
     * - ReadWriteLock
     * - Object monitor
     * - etc.
     */

    private final Lock lock = new ReentrantLock();
    private final Condition sufficientFunds = lock.newCondition();

    public BankAccount(int accountId, long initialBalance) {
        this.accountId = accountId;
        this.balance = initialBalance;
    }

    public int getAccountId() {
        return accountId;
    }

    /*
     * TODO:
     * Return the current balance in a thread-safe way.
     *
     * Requirements:
     * - Must be safe under concurrent reads/writes
     * - Should not block unnecessarily if using read/write locks
     */
    public long getBalance() {
        lock.lock();
        try {
            return balance;
        }
        finally {
           lock.unlock();
        }
    }

    /*
     * TODO:
     * Increase balance atomically.
     *
     * Requirements:
     * - Must not lose updates under concurrency
     */
    public void deposit(long amount) {
        if(amount <= 0){
            return;
        }
        lock.lock();
        try {
            this.balance += amount;
            sufficientFunds.signalAll();
        }
        finally {
            lock.unlock();
        }
    }

    /*
     * TODO:
     * Decrease balance atomically.
     *
     * Requirements:
     * - Must not cause race conditions
     * - Negative balance handling is NOT required unless you decide
     *   to extend the system (optional)
     */
    public void withdraw(long amount) {
        if(amount <= 0){
            return;
        }
        lock.lock();
        try{
            while (this.balance < amount)
            {
                sufficientFunds.await();
            }
            this.balance -= amount;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    /*
     * TODO:
     * Transfer money between two accounts atomically.
     *
     * IMPORTANT REQUIREMENTS:
     * - Must be atomic (no partial transfer)
     * - Must be deadlock-free
     * - Must protect both source and target accounts
     *
     * HINT:
     * - Consider global lock ordering using accountId
     * - Or tryLock with retry strategy
     */
    public void transfer(BankAccount target, long amount) {
//        BankAccount first = this.accountId < target.accountId ? this : target;
//        BankAccount second = this.accountId < target.accountId ? target : this;
//
//        first.lock.lock();
//        try{
//            second.lock.lock();
//            try{
//                this.withdraw(amount);
//                target.deposit(amount);
//            }
//            finally {
//                second.lock.unlock();
//            }
//        }
//        finally {
//            first.lock.unlock();
//        }

        //if we implement conditional waiting
        this.withdraw(amount);
        target.deposit(amount);
    }
}