<?php
class Customer {
    private $conn;
    private $table_name = "clients";

    public $id;
    public $y_tunnus;
    public $company_name;
    public $first_name;
    public $last_name;
    public $email;
    public $phone;
    public $address;
    public $city;
    public $state;
    public $postal_code;
    public $country;
    public $notes;
    public $hour_price;
    public $created_at;
    public $updated_at;

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET y_tunnus=:y_tunnus, company_name=:company_name, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, address=:address, city=:city, state=:state, postal_code=:postal_code, country=:country, notes=:notes, hour_price=:hour_price, created_at=:created_at, updated_at=:updated_at";

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

        $this->y_tunnus = htmlspecialchars(strip_tags($this->y_tunnus));
        $this->company_name = htmlspecialchars(strip_tags($this->company_name));
        $this->first_name = htmlspecialchars(strip_tags($this->first_name));
        $this->last_name = htmlspecialchars(strip_tags($this->last_name));
        $this->email = htmlspecialchars(strip_tags($this->email));
        $this->phone = htmlspecialchars(strip_tags($this->phone));
        $this->address = htmlspecialchars(strip_tags($this->address));
        $this->city = htmlspecialchars(strip_tags($this->city));
        $this->state = htmlspecialchars(strip_tags($this->state));
        $this->postal_code = htmlspecialchars(strip_tags($this->postal_code));
        $this->country = htmlspecialchars(strip_tags($this->country));
        $this->notes = htmlspecialchars(strip_tags($this->notes));
        $this->hour_price = htmlspecialchars(strip_tags($this->hour_price));
        $this->created_at = date('Y-m-d H:i:s');
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":y_tunnus", $this->y_tunnus);
        $stmt->bindParam(":company_name", $this->company_name);
        $stmt->bindParam(":first_name", $this->first_name);
        $stmt->bindParam(":last_name", $this->last_name);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":phone", $this->phone);
        $stmt->bindParam(":address", $this->address);
        $stmt->bindParam(":city", $this->city);
        $stmt->bindParam(":state", $this->state);
        $stmt->bindParam(":postal_code", $this->postal_code);
        $stmt->bindParam(":country", $this->country);
        $stmt->bindParam(":notes", $this->notes);
        $stmt->bindParam(":hour_price", $this->hour_price);
        $stmt->bindParam(":created_at", $this->created_at);
        $stmt->bindParam(":updated_at", $this->updated_at);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function read() {
        $query = "SELECT * FROM " . $this->table_name . " ORDER BY company_name, last_name, first_name";

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

        return $stmt;
    }

    public function readOne() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        $this->y_tunnus = $row['y_tunnus'];
        $this->company_name = $row['company_name'];
        $this->first_name = $row['first_name'];
        $this->last_name = $row['last_name'];
        $this->email = $row['email'];
        $this->phone = $row['phone'];
        $this->address = $row['address'];
        $this->city = $row['city'];
        $this->state = $row['state'];
        $this->postal_code = $row['postal_code'];
        $this->country = $row['country'];
        $this->notes = $row['notes'];
        $this->hour_price = $row['hour_price'];
        $this->created_at = $row['created_at'];
        $this->updated_at = $row['updated_at'];
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . " SET y_tunnus=:y_tunnus, company_name=:company_name, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, address=:address, city=:city, state=:state, postal_code=:postal_code, country=:country, notes=:notes, hour_price=:hour_price, updated_at=:updated_at WHERE id=:id";

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

        $this->y_tunnus = htmlspecialchars(strip_tags($this->y_tunnus));
        $this->company_name = htmlspecialchars(strip_tags($this->company_name));
        $this->first_name = htmlspecialchars(strip_tags($this->first_name));
        $this->last_name = htmlspecialchars(strip_tags($this->last_name));
        $this->email = htmlspecialchars(strip_tags($this->email));
        $this->phone = htmlspecialchars(strip_tags($this->phone));
        $this->address = htmlspecialchars(strip_tags($this->address));
        $this->city = htmlspecialchars(strip_tags($this->city));
        $this->state = htmlspecialchars(strip_tags($this->state));
        $this->postal_code = htmlspecialchars(strip_tags($this->postal_code));
        $this->country = htmlspecialchars(strip_tags($this->country));
        $this->notes = htmlspecialchars(strip_tags($this->notes));
        $this->hour_price = htmlspecialchars(strip_tags($this->hour_price));
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":y_tunnus", $this->y_tunnus);
        $stmt->bindParam(":company_name", $this->company_name);
        $stmt->bindParam(":first_name", $this->first_name);
        $stmt->bindParam(":last_name", $this->last_name);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":phone", $this->phone);
        $stmt->bindParam(":address", $this->address);
        $stmt->bindParam(":city", $this->city);
        $stmt->bindParam(":state", $this->state);
        $stmt->bindParam(":postal_code", $this->postal_code);
        $stmt->bindParam(":country", $this->country);
        $stmt->bindParam(":notes", $this->notes);
        $stmt->bindParam(":hour_price", $this->hour_price);
        $stmt->bindParam(":updated_at", $this->updated_at);
        $stmt->bindParam(":id", $this->id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function search($search_term) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE 
                  y_tunnus LIKE ? OR 
                  company_name LIKE ? OR 
                  first_name LIKE ? OR 
                  last_name LIKE ? OR 
                  email LIKE ? OR 
                  phone LIKE ? OR 
                  address LIKE ? OR 
                  city LIKE ? OR 
                  state LIKE ? OR 
                  postal_code LIKE ? OR 
                  country LIKE ? OR 
                  notes LIKE ?
                  ORDER BY company_name, last_name, first_name";

        $stmt = $this->conn->prepare($query);
        $search_term = "%{$search_term}%";
        $stmt->bindParam(1, $search_term);
        $stmt->bindParam(2, $search_term);
        $stmt->bindParam(3, $search_term);
        $stmt->bindParam(4, $search_term);
        $stmt->bindParam(5, $search_term);
        $stmt->bindParam(6, $search_term);
        $stmt->bindParam(7, $search_term);
        $stmt->bindParam(8, $search_term);
        $stmt->bindParam(9, $search_term);
        $stmt->execute();

        return $stmt;
    }

    public function getProjects($customer_id) {
        $query = "SELECT * FROM projects WHERE customer_id = ? ORDER BY start_date DESC, created_at DESC";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $customer_id);
        $stmt->execute();

        return $stmt;
    }

    public function getDisplayName() {
        if (!empty($this->company_name)) {
            return $this->company_name . ' - ' . $this->first_name . ' ' . $this->last_name;
        } else {
            return $this->first_name . ' ' . $this->last_name;
        }
    }

    public function getPrimaryContact() {
        $query = "SELECT * FROM contact_persons WHERE client_id = ? AND is_primary = TRUE LIMIT 1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row ? $row['first_name'] . ' ' . $row['last_name'] : 'No primary contact';
    }
}
?>
