from app.extensions import db
from datetime import datetime


class TransBorrow(db.Model):
    __tablename__ = 'trans_borrow'

    id = db.Column(db.Integer, primary_key=True)
    borrow_no = db.Column(db.String(100))
    sku = db.Column(db.String(100))
    source_table = db.Column(db.String(50))
    stock_id = db.Column(db.Integer)
    barcode = db.Column(db.String(100))
    quantity = db.Column(db.Numeric(19, 4))
    borrower_name = db.Column(db.String(100))
    borrow_time = db.Column(db.DateTime, default=datetime.now)
    borrow_signature = db.Column(db.Text)
    expected_return_time = db.Column(db.DateTime)
    is_returned = db.Column(db.Boolean, default=False)
    return_time = db.Column(db.DateTime)
    return_operator = db.Column(db.String(100))
    return_signature = db.Column(db.Text)
    return_location = db.Column(db.String(100))
    status = db.Column(db.String(20), default='borrowed')
    remark = db.Column(db.Text)

    def to_dict(self):
        return {
            'id': self.id,
            'borrow_no': self.borrow_no,
            'sku': self.sku,
            'source_table': self.source_table,
            'stock_id': self.stock_id,
            'barcode': self.barcode,
            'quantity': float(self.quantity) if self.quantity is not None else None,
            'borrower_name': self.borrower_name,
            'borrow_time': self.borrow_time.strftime('%Y-%m-%d %H:%M') if self.borrow_time else None,
            'borrow_signature': self.borrow_signature,
            'expected_return_time': self.expected_return_time.strftime('%Y-%m-%d %H:%M') if self.expected_return_time else None,
            'is_returned': self.is_returned,
            'return_time': self.return_time.strftime('%Y-%m-%d %H:%M') if self.return_time else None,
            'return_operator': self.return_operator,
            'return_signature': self.return_signature,
            'return_location': self.return_location,
            'status': self.status,
            'remark': self.remark,
        }


class TransRepair(db.Model):
    __tablename__ = 'trans_repair'

    id = db.Column(db.Integer, primary_key=True)
    sku = db.Column(db.String(100))
    source_table = db.Column(db.String(50))
    stock_id = db.Column(db.Integer)
    arrival_date = db.Column(db.Date)
    expected_repair_time = db.Column(db.String(100))
    shipping_date = db.Column(db.Date)
    is_self_made = db.Column(db.Boolean, default=False)
    related_product_id = db.Column(db.Integer)
    related_contract_id = db.Column(db.String(100))
    repair_manager = db.Column(db.String(100))
    fault_description = db.Column(db.Text)
    repair_result = db.Column(db.Text)
    cost_price = db.Column(db.Numeric(19, 4))
    sale_price = db.Column(db.Numeric(19, 4))

    def to_dict(self):
        return {
            'id': self.id,
            'sku': self.sku,
            'source_table': self.source_table,
            'stock_id': self.stock_id,
            'arrival_date': self.arrival_date.strftime('%Y-%m-%d') if self.arrival_date else None,
            'expected_repair_time': self.expected_repair_time,
            'shipping_date': self.shipping_date.strftime('%Y-%m-%d') if self.shipping_date else None,
            'is_self_made': self.is_self_made,
            'related_product_id': self.related_product_id,
            'related_contract_id': self.related_contract_id,
            'repair_manager': self.repair_manager,
            'fault_description': self.fault_description,
            'repair_result': self.repair_result,
            'cost_price': float(self.cost_price) if self.cost_price is not None else None,
            'sale_price': float(self.sale_price) if self.sale_price is not None else None,
        }


class TransScrap(db.Model):
    __tablename__ = 'trans_scrap'

    id = db.Column(db.Integer, primary_key=True)
    sku = db.Column(db.String(100))
    source_table = db.Column(db.String(50))
    stock_id = db.Column(db.Integer)
    quantity = db.Column(db.Numeric(19, 4))
    reason = db.Column(db.Text)
    operator_name = db.Column(db.String(100))
    operation_time = db.Column(db.DateTime, default=datetime.now)
    approver_name = db.Column(db.String(100))
    approval_status = db.Column(db.String(20), default='pending')
    cost_at_scrap = db.Column(db.Numeric(19, 4))
    total_loss = db.Column(db.Numeric(19, 4))

    def to_dict(self):
        return {
            'id': self.id,
            'sku': self.sku,
            'source_table': self.source_table,
            'stock_id': self.stock_id,
            'quantity': float(self.quantity) if self.quantity is not None else None,
            'reason': self.reason,
            'operator_name': self.operator_name,
            'operation_time': self.operation_time.strftime('%Y-%m-%d %H:%M:%S') if self.operation_time else None,
            'approver_name': self.approver_name,
            'approval_status': self.approval_status,
            'cost_at_scrap': float(self.cost_at_scrap) if self.cost_at_scrap is not None else None,
            'total_loss': float(self.total_loss) if self.total_loss is not None else None,
        }
