<?php

require_once 'Logger.php';

class Branch {
    private $conn;
    private $logger;
    private $table_name = "branches";

    public $branch_id;
    public $branch_name;
    public $branch_address;
    public $branch_phone;
    public $branch_email;
    public $branch_website;
    public $branch_logo;
    public $branch_company_name;


    public function __construct($db, $logger = null) {
        $this->conn = $db;
        $this->logger = $logger;
    }

    public function create() {
        $query = "SELECT COUNT(*) FROM " . $this->table_name . " WHERE branch_name = :branch_name";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':branch_name', $this->branch_name);
        $stmt->execute();

        if ($stmt->fetchColumn() > 0) {
            return false;
        }

        $query = "INSERT INTO " . $this->table_name . " (branch_name, branch_address, branch_phone, branch_email, branch_website, branch_logo, branch_company_name)
                  VALUES (:branch_name, :branch_address, :branch_phone, :branch_email, :branch_website, :branch_logo, :branch_company_name)";
        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(':branch_name', $this->branch_name);
        $stmt->bindParam(':branch_address', $this->branch_address);
        $stmt->bindParam(':branch_phone', $this->branch_phone);
        $stmt->bindParam(':branch_email', $this->branch_email);
        $stmt->bindParam(':branch_website', $this->branch_website);
        $stmt->bindParam(':branch_logo', $this->branch_logo);
        $stmt->bindParam(':branch_company_name', $this->branch_company_name);

        if ($stmt->execute()) {
            $this->branch_id = $this->conn->lastInsertId();

            if ($this->logger) {
                $branchData = [
                    'branch_name' => $this->branch_name,
                    'branch_address' => $this->branch_address,
                    'branch_phone' => $this->branch_phone,
                    'branch_email' => $this->branch_email,
                    'branch_website' => $this->branch_website,
                    'branch_logo' => $this->branch_logo,
                    'branch_company_name' => $this->branch_company_name,
                ];
                $this->logger->logBranch('CREATE', $this->branch_id, $branchData);
            }

            return true;
        }
        return false;
    }

    public function read() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE branch_id = :branch_id";
        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(':branch_id', $this->branch_id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($row) {
            $this->branch_name = $row['branch_name'];
            $this->branch_address = $row['branch_address'];
            $this->branch_phone = $row['branch_phone'];
            $this->branch_email = $row['branch_email'] ?? '';
            $this->branch_website = $row['branch_website'] ?? '';
            $this->branch_logo = $row['branch_logo'] ?? '';
            $this->branch_company_name = $row['branch_company_name'] ?? '';
            return true;
        }
        return false;
    }

    public function getAllBranches() {
        $query = "SELECT * FROM " . $this->table_name;
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }

    public function update() {
        $oldData = null;
        if ($this->logger) {
            $oldData = $this->getBranchById($this->branch_id);
        }

        $query = "UPDATE " . $this->table_name . "
                SET branch_name = :branch_name,
                    branch_address = :branch_address,
                    branch_phone = :branch_phone,
                    branch_email = :branch_email,
                    branch_website = :branch_website,
                    branch_logo = :branch_logo,
                    branch_company_name = :branch_company_name
                WHERE branch_id = :branch_id";

        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(':branch_name', $this->branch_name);
        $stmt->bindParam(':branch_address', $this->branch_address);
        $stmt->bindParam(':branch_phone', $this->branch_phone);
        $stmt->bindParam(':branch_email', $this->branch_email);
        $stmt->bindParam(':branch_website', $this->branch_website);
        $stmt->bindParam(':branch_logo', $this->branch_logo);
        $stmt->bindParam(':branch_company_name', $this->branch_company_name);
        $stmt->bindParam(':branch_id', $this->branch_id);

        $result = $stmt->execute();

        if ($result && $this->logger && $oldData) {
            $newData = [
                'branch_name' => $this->branch_name,
                'branch_address' => $this->branch_address,
                'branch_phone' => $this->branch_phone,
                'branch_email' => $this->branch_email,
                'branch_website' => $this->branch_website,
                'branch_logo' => $this->branch_logo,
                'branch_company_name' => $this->branch_company_name,
            ];
            $this->logger->logBranch('UPDATE', $this->branch_id, $newData, $oldData);
        }

        return $result;
    }

    public function delete() {
        $branchData = null;
        if ($this->logger) {
            $branchData = $this->getBranchById($this->branch_id);
        }

        $query = "DELETE FROM " . $this->table_name . " WHERE branch_id = :branch_id";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':branch_id', $this->branch_id);

        $result = $stmt->execute();

        if ($result && $this->logger && $branchData) {
            $this->logger->logBranch('DELETE', $this->branch_id, null, $branchData);
        }

        return $result;
    }

    public function getBranchById($branch_id) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE branch_id = :branch_id";
        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(':branch_id', $branch_id);
        $stmt->execute();

        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function getBranchByName($branchName) {
        $sql = "SELECT * FROM " . $this->table_name . " WHERE LOWER(branch_name) = LOWER(:branch_name) LIMIT 1";
        try {
            $stmt = $this->conn->prepare($sql);
            $stmt->bindParam(':branch_name', $branchName, PDO::PARAM_STR);
            $stmt->execute();
            $branch = $stmt->fetch(PDO::FETCH_ASSOC);
            return $branch;
        } catch (PDOException $e) {
            return false;
        }
    }

    public function updateContacts($branchId, $address, $phone, $email, $website) {
        $oldData = $this->getBranchById($branchId);

        $query = "UPDATE " . $this->table_name . "
                SET branch_address = :branch_address,
                    branch_phone = :branch_phone,
                    branch_email = :branch_email,
                    branch_website = :branch_website
                WHERE branch_id = :branch_id";

        $stmt = $this->conn->prepare($query);
        $stmt->bindValue(':branch_address', trim((string)$address));
        $stmt->bindValue(':branch_phone', trim((string)$phone));
        $stmt->bindValue(':branch_email', trim((string)$email));
        $stmt->bindValue(':branch_website', trim((string)$website));
        $stmt->bindValue(':branch_id', (int)$branchId, PDO::PARAM_INT);

        $result = $stmt->execute();

        if ($result && $this->logger && $oldData) {
            $newData = $this->getBranchById($branchId);
            $this->logger->logBranch('UPDATE_CONTACTS', $branchId, $newData, $oldData);
        }

        return $result;
    }
}
?>
